Thread overview
Type Unions for C#
Aug 03
harakim
July 30

D and C# share some similarities and I found this an interesting C# proposal, which we can use to see if our current plans are the best or if they improved something.

https://github.com/dotnet/csharplang/blob/18a527bcc1f0bdaf542d8b9a189c50068615b439/proposals/TypeUnions.md

August 03

On Tuesday, 30 July 2024 at 07:24:00 UTC, Daniel Nielsen wrote:

>

D and C# share some similarities and I found this an interesting C# proposal, which we can use to see if our current plans are the best or if they improved something.

https://github.com/dotnet/csharplang/blob/18a527bcc1f0bdaf542d8b9a189c50068615b439/proposals/TypeUnions.md

Java enums are the only feature that of Java I like better than C#. An enum, in my mind, should be a list of all possible values. That falls flat in C# because you can have someting like this

Animal a = (Animal)200;

and it compiles just fine. I have had a lot of trouble with that when deserializing data. Also, he is right that you can't say int b = a switch { Animal.Dog => 1; Animal.Cat => 2 } because the enum provides no guarantees about what will be stored.

Enums that guarantee it is one of many values are great. They simplify code so much. C# enums are like missing type safety compared to Java. I have even seen Java enums used for strategy pattern, where you store the name in the database and then it loads the enum without explicit reflection. It made me double take at first, but turned out to be awesome.

August 06

On Saturday, 3 August 2024 at 11:59:29 UTC, harakim wrote:

>

Java enums are the only feature that of Java I like better than C#. An enum, in my mind, should be a list of all possible values. That falls flat in C# because you can have someting like this

Animal a = (Animal)200;

and it compiles just fine. I have had a lot of trouble with that when deserializing data. Also, he is right that you can't say int b = a switch { Animal.Dog => 1; Animal.Cat => 2 } because the enum provides no guarantees about what will be stored.

Enums that guarantee it is one of many values are great. They simplify code so much. C# enums are like missing type safety compared to Java. I have even seen Java enums used for strategy pattern, where you store the name in the database and then it loads the enum without explicit reflection. It made me double take at first, but turned out to be awesome.

That is because in Java, enum values are actually objects, whereas in C# (like in C, C++, and usually in D), enum values are integers. (In D, enum values can have any type.)

You can emulate Java enums (to some degree) in D using a struct and static immutable values.