LukeJ
LukeJ
CC#
Created by LukeJ on 1/23/2024 in #help
Problems with loading symbols on Mac Studio
Hello. Since getting this mac for work I've had problems getting vscode to work right in Unity. It doesn't seem to know about a bunch of scripts it should so intellisense struggles and some scripts outline is simply empty. When I load up vs with the project, I can see the correct highlighting for a second, before it then corrects and isn't attached to unity at all. It also asks me about two different workspaces, which seem to change nothing. I think this comes from a botched attempt at installing dotnet and further confusion caused problems trying to fix that (I'm not used to mac, yet alone the differences running on the new silicone). In the end I got it working on a test project, but not the actual long existing project I'm working with, and so gave up and worked with other tools just to keep moving. I can't do that now though so need to get this fixed and am lost with it. My current thought is to try reinstall all my dotnet installs (since I know some aren't supposed to be there), but the uninstall tool isn't working. I download the tar.gz, run the given command, it shows me the help for the tool, then trying to run anything fails to find the command. Since it's been a while since last messing with this that's about as far as I remember what to try do. I know I already tried reinstalling vscode last time and that made no difference. Any ideas are appreciated since this is now a major obstacle to my work. Thanks.
3 replies
CC#
Created by LukeJ on 12/18/2023 in #help
Find invocation in call stack
Hello. I'm currently trying to understand why someone else's code works, and found a very important function I've got a breakpoint on. The problem is, though, that the call stack just goes back to a Unity coroutine, and I have no idea where the code running this lives. For invocations in general but possibly Unity coroutines more specifically, how can I find the source? The call stack dead ends at that invocation and I can't see anything about what made it.
1 replies
CC#
Created by LukeJ on 12/7/2023 in #help
.NET SDK problems adding to PATH on Mac
Hello. I've recently gotten a Mac for work and am having troubles setting up my environment properly. I need to use .Net, but my IDE (VS Code) isn't finding it properly. In the terminal if I run dotnet I get nothing, and which dotnet can't find it either. I've added the correct file to PATH in just about every way of writing it I can think of (it's now a mess 😓 ) and still nothing. I'm not sure what to do.
44 replies
CC#
Created by LukeJ on 2/25/2023 in #help
✅ LanguageExt bind if
I'm having trouble understanding how to do something particular in LanguageExt that I think is just a part of functional programming in general. I have an effectful method, which I then want to evaluate the result of, and rerun if not meeting a condition. Originally, I did it like this
var key = PollInput<RT>();

return key.Bind(pressedKey => pressedKey.Match(
Left: keys.Contains,
Right: _ => true)
? key
: IO<RT>.ClearCharacter()
.Bind(_ => PollValidInput<RT>(keys)));
var key = PollInput<RT>();

return key.Bind(pressedKey => pressedKey.Match(
Left: keys.Contains,
Right: _ => true)
? key
: IO<RT>.ClearCharacter()
.Bind(_ => PollValidInput<RT>(keys)));
Turns out, obviously enough, it runs PollInput twice when you do this, which isn't good. Instead, I'm now trying to do something like this
from keyPressed in PollInput<RT>()
from _ in keyPressed.Match(
Left: keys.Contains,
Right: _ => true)
? keyPressed
: IO<RT>.ClearCharacter()
.Bind(_ => PollValidInput<RT>(keys));
from keyPressed in PollInput<RT>()
from _ in keyPressed.Match(
Left: keys.Contains,
Right: _ => true)
? keyPressed
: IO<RT>.ClearCharacter()
.Bind(_ => PollValidInput<RT>(keys));
But this doesn't work, since keyPressed is not an Eff, and PollValidInput (the method this is running in), is an Eff. I have no idea how to do this. I just want to bind that else condition when that match fails, but I need to bind/map to run that condition, so I also need to return something for when that condition is met. Any ideas?
2 replies
CC#
Created by LukeJ on 2/23/2023 in #help
✅ Can no longer run console app after adding unit tests
I made a little test project that I've slowly been building onto. It's gotten big enough now that I've added NUnit unit tests to make it easier, but I seem to have done it wrong and now the app won't open. When I hit run it brings up a configuration window with the error on the bottom Error: Project has no runnable target frameworks defined. The Target framework parameter shows net6.0 though, so I'm not sure what the problem is. Here's what the csproj file looks like
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LanguageExt.Core" Version="4.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0-preview-20221221-03" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit.Console" Version="3.16.2" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LanguageExt.Core" Version="4.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0-preview-20221221-03" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit.Console" Version="3.16.2" />
</ItemGroup>

</Project>
I added in the Microsoft.NET.Test.Sdk to get the unit tests to appear in the test runner, but it also causes this error running it normally. Comparing it to a freshly made unit test project, the csproj looks about the same. I've definitely done something wrong in adding the nunit packages, but I have no idea what.
13 replies
CC#
Created by LukeJ on 2/22/2023 in #help
✅ Record for ensuring parameters
Hello. I'm currently learning Function C#, and the guy I'm currently making a demo for gave me a tip to use a record to ensure any maps of enums will be safe in that they can't compile with different entries than the record contains, so you only ever have to update that one record to never miss a map to update. He showed me this code as an example of it:
record TurnMoveA<A>(A spawnWarrior, A spawnArcher, A spawnCleric) {
public A this[TurnMove turnMove] => /* ... */;
}
record TurnMoveA<A>(A spawnWarrior, A spawnArcher, A spawnCleric) {
public A this[TurnMove turnMove] => /* ... */;
}
Generic, of course, because this would map from the TurnMove enum to anything else you wanted to map it to. I'm not actually sure how to do this in practice though, since this is the best I can come up with in implementing something that uses a record like this
public record TurnMoveBinding(TurnMove zKey, TurnMove xKey, TurnMove cKey) : ConsoleKeyBinding<TurnMove>(zKey, xKey, cKey)
{
public Option<TurnMove> this[ConsoleKey key] => key switch
{
ConsoleKey.Z => TurnMove.SpawnWarrior,
ConsoleKey.X => TurnMove.SpawnArcher,
ConsoleKey.C => TurnMove.SpawnCleric,
_ => Option<TurnMove>.None
};
}
public record TurnMoveBinding(TurnMove zKey, TurnMove xKey, TurnMove cKey) : ConsoleKeyBinding<TurnMove>(zKey, xKey, cKey)
{
public Option<TurnMove> this[ConsoleKey key] => key switch
{
ConsoleKey.Z => TurnMove.SpawnWarrior,
ConsoleKey.X => TurnMove.SpawnArcher,
ConsoleKey.C => TurnMove.SpawnCleric,
_ => Option<TurnMove>.None
};
}
You can ignore the option stuff as a part of LanguageExt. My problem here is to use it, you have to instance it, and therefore have to fill in those parameters. Those parameters are only there for parity though, and I'm not sure how you could avoid having them. What am I missing here? Before I get any messages in this vein, by the way, I'm sure there's a more sensible way of doing this, or some reason to not bother in the first place. I have been recommended to do this though, and so in the interest of learning, I want to make this work. They use it in their system, with the help of some tooling, so it's practical for me to know.
1 replies
CC#
Created by LukeJ on 2/22/2023 in #help
✅ Typed delegates in maps (LanguageExt)
Hello. I'm currently doing some functional programming with the LanguageExt package. I've been going through and adding an environment to my effectful methods, which has left me with a bit of a problem. I have 3 different turn methods that I store in in map, like so
private static readonly Map<ConsoleKey, TurnMethod> TurnMethodBinding = Map(
(ConsoleKey.Z, (TurnMethod)UserTurn),
(ConsoleKey.X, AITurn),
(ConsoleKey.C, AITurnInPhases)
);
private static readonly Map<ConsoleKey, TurnMethod> TurnMethodBinding = Map(
(ConsoleKey.Z, (TurnMethod)UserTurn),
(ConsoleKey.X, AITurn),
(ConsoleKey.C, AITurnInPhases)
);
This uses the delegate TurnMethod defined as
public delegate Eff<RT, Either<TurnReturn, ExitConsole>> TurnMethod<RT>(FieldState fieldState, User player) where RT : struct, HasIO<RT>;
public delegate Eff<RT, Either<TurnReturn, ExitConsole>> TurnMethod<RT>(FieldState fieldState, User player) where RT : struct, HasIO<RT>;
As you can see though, this is typed with RT, which got added to the signature when adding an environment to the method. This causes the Map problems, saying there's an incorrect number of type paramaters. I have no idea how to do that map with the type paramater included though. Any ideas?
17 replies
CC#
Created by LukeJ on 2/9/2023 in #help
✅ Unit tests not appearing
Hello! For once I'm doing a little project in the console rather than in Unity, meaning for unit tests I'm having to add the package myself. I've added the NuGet packages NUnit and NUnit.Console, and I've written a test file that doesn't give any complains. When I try add run it though, the Unit Test tab of Rider show's no tests. I can't figure out why though. All I can think of is the assembly not being referenced, but Rider says it does that automatically for NUnit packages. Any ideas? I'm sure it's something simple didn't know was a step. Here's the test class, in case I've done something wrong there.
using NUnit.Framework;

[TestFixture]
public class UnitTests
{
[Test]
public void Test1()
{
Assert.That(4, Is.EqualTo(2 + 2));
}
}
using NUnit.Framework;

[TestFixture]
public class UnitTests
{
[Test]
public void Test1()
{
Assert.That(4, Is.EqualTo(2 + 2));
}
}
12 replies
CC#
Created by LukeJ on 12/4/2022 in #help
✅ Type shown that isn't present
3 replies
CC#
Created by LukeJ on 12/4/2022 in #help
✅ Cannot convert from anonymous type
I have a method that takes effectively extends OrderBy, and so one of it's paramaters is a keySelector, just like in OrderBy
Func<TElement, TKey> selector
Func<TElement, TKey> selector
In my use case I separated out the intended key to make it read better, so here is it's signature
private static int SelectByDistanceFromEnemy(this (Option<Unit> unit, int index) units)
private static int SelectByDistanceFromEnemy(this (Option<Unit> unit, int index) units)
I'm then using it in this code (lambda to call SelectByDistanceFromEnemy just to try make the error easier to parse)
nits.Select((unit, index) => new { unit, index })
.RunOrderedDescending(tpl => SelectByDistanceFromEnemy(tpl),
...
nits.Select((unit, index) => new { unit, index })
.RunOrderedDescending(tpl => SelectByDistanceFromEnemy(tpl),
...
which gives me this error cannot convert from '<anonymous type: LanguageExt.Option<Unit> unit, int index>' to '(LanguageExt.Option<Unit> unit, int index) I don't get why it can't though. The only difference is one is an anonymous type. Anyone know why?
40 replies
CC#
Created by LukeJ on 12/2/2022 in #help
✅ Invalid cast between interface types
Hello. I'm trying to learn functional programming in C#, so I'm ending up with some unusual code. A part of that is having an array of options marked with their length, like so
IMarked<Arr<Option<Unit>>, CountOfField>
IMarked<Arr<Option<Unit>>, CountOfField>
Since this is so long, I've defined this
public interface FieldUnits : IMarked<Arr<Option<Unit>>, CountOfField> {}
public interface FieldUnits : IMarked<Arr<Option<Unit>>, CountOfField> {}
This inevitably means casting to FieldUnits at some point, and doing so is giving me this error
System.InvalidCastException: 'Unable to cast object of type 'Marked`2[LanguageExt.Arr`1[LanguageExt.Option`1[Unit]],CountOfField]' to type 'FieldUnits'.'
System.InvalidCastException: 'Unable to cast object of type 'Marked`2[LanguageExt.Arr`1[LanguageExt.Option`1[Unit]],CountOfField]' to type 'FieldUnits'.'
I'm not sure why though, since they seem to be the same. I also swear this was working the other day. Any ideas what's wrong here? Here's a sample line that has this problem
var units = (FieldUnits)Enumerable.Repeat(Option<Unit>.None, CountOfField.Count).ToArr().mark(default(CountOfField));
var units = (FieldUnits)Enumerable.Repeat(Option<Unit>.None, CountOfField.Count).ToArr().mark(default(CountOfField));
24 replies
CC#
Created by LukeJ on 11/19/2022 in #help
❔ Inconsistent accessibility [Answered]
I have this bit of code here
enum UnitName {
Warrior,
Archer,
Cleric
}

public readonly record struct Unit(char indicator, int cost, int damage, int hp, int range);

public static class Units {
public static readonly Map<UnitName, Unit> all = Map(
(UnitName.Warrior, new Unit('x', 15, 4, 9, 1)),
(UnitName.Archer, new Unit('x', 20, 2, 5, 4)),
(UnitName.Cleric, new Unit('x', 30, 1, 3, 6))
);
}
enum UnitName {
Warrior,
Archer,
Cleric
}

public readonly record struct Unit(char indicator, int cost, int damage, int hp, int range);

public static class Units {
public static readonly Map<UnitName, Unit> all = Map(
(UnitName.Warrior, new Unit('x', 15, 4, 9, 1)),
(UnitName.Archer, new Unit('x', 20, 2, 5, 4)),
(UnitName.Cleric, new Unit('x', 30, 1, 3, 6))
);
}
And I cannot for the life of me figure out why it gives me the error Inconsistent accessibility: field type 'Map<UnitName, Unit>' is less accessible than field 'Units.all. Map is just an immutable dictionary from Language.Ext (https://louthy.github.io/language-ext/LanguageExt.Core/Immutable%20Collections/Map/index.html) Any ideas?
6 replies
CC#
Created by LukeJ on 10/13/2022 in #help
Cannot set readonly field in inherited constructor [Answered]
Title says it all really, I'm not really sure why given this is clearly a constructor.
public class StandardCard : Card {
public StandardCard(Suite suite, Rank rank) {
if (!StandardRanks.ranks.Contains(rank))
throw new Exception();

this.suite = suite;
this.rank = rank;
}
}
public class StandardCard : Card {
public StandardCard(Suite suite, Rank rank) {
if (!StandardRanks.ranks.Contains(rank))
throw new Exception();

this.suite = suite;
this.rank = rank;
}
}
12 replies
CC#
Created by LukeJ on 10/13/2022 in #help
Cannot create comparer [Answered]
I'm trying to make a comparer to pass around but it's refusing to let me define it. Here's the code
Comparer<Card> trumpComparison = new Func<Card, Card, int>((card1, card2) => (card1.suite == trump ? 1 : 0) - (card2.suite == trump ? 1 : 0));
Comparer<Card> trumpComparison = new Func<Card, Card, int>((card1, card2) => (card1.suite == trump ? 1 : 0) - (card2.suite == trump ? 1 : 0));
Card in this case being
public readonly record struct Card(Suite suite, Rank rank);
public readonly record struct Card(Suite suite, Rank rank);
I've tried doing it as a lambda and it complained it wasn't a delegate. So instead I give it a func, and now it's telling me it cannot convert it to a comparer. What am I missing here?
4 replies
CC#
Created by LukeJ on 10/13/2022 in #help
Adding a library [Answered]
Hello. I very rarely work in the console, but now I've started doing so I've found myself needing to add a library. I can't for the life of me figure out how to do so, though. I keep seeing to add the dll in the references, but I don't see any reference file in my solution. As far as I can tell I just don't have one in this project. What am I missing here?
12 replies
CC#
Created by LukeJ on 10/12/2022 in #help
Can't access record members [Answered]
I have a record that looks like this
public readonly record struct Card(Suite suite, Rank rank);
public readonly record struct Card(Suite suite, Rank rank);
And am making a record that looks like this
public readonly record struct StandardCard<Card> {
public readonly Card card;

public StandardCard(Card card) {
this.card = card;
}
}
public readonly record struct StandardCard<Card> {
public readonly Card card;

public StandardCard(Card card) {
this.card = card;
}
}
I'm new to messing with records, so can't figure something out. Why can't I do card.suite or card.rank in that constructor? I can do it elsewhere in my doe, but not here.
15 replies
CC#
Created by LukeJ on 9/27/2022 in #help
Collection of different classes
3 replies