This question is not about C#/F# compatibility as in this one.
I'd like to know the proper way to implement a type like F# Unit in place of using void.
Obviously I'll discard this result and I'll not expose this type to the outside world.
Wondering if an empty class could impersonate this role.
internal class Unit
{
}
For example in language-ext library, the author used a struct.
Is there any benefit in this choice?
I'm not sure what is the best way to define
Unitfor usage from C#. It might differ from how this is done in F# (because in F#, the compiler hides the usage in a way).However, you can actually find the implementation of F#
unitin the core library:prim-types.fsiprim-types.fsHere are the key points about the F#
unitimplementationGetHashCodeandEqualsin the same way to the Rx versionIComparableand all values of theunittype are equalunitvalue. It also has no default instance (unlike the Rx unit) and so in F#, all unit values are actually represented asnull. However, the language/compiler generally hide this fact.So, it sounds like the only difference in F# is that it uses the
nullvalue. If you want to useunitexplicitly, this might not be the best choice. However, if you haveUnit.Defaultthen you are ultimately defining a type with two possible values, because it can be eitherUnit.Defaultornull(and so it is not really a unit!)