F# Types Docs said that:
Unit Type: Describes the
unittype, a type that has one value and that is indicated by (); equivalent tovoidin C# andNothingin Visual Basic.
It said they're equal, but why does the following comparison return false?
typedefof<System.Void> = typedefof<unit>;;
val it : bool = false
Well, they're not really equivalent. The quote you posted is a bit misleading.
The F# type unit is equivalent to C# keyword void (not the type System.Void!) in that both the type and the keyword are used to declare functions that don't return anything:
// F#
let f (x:int) : unit = ...
// C#
void f( int x ) { ... }
This is what your quote is talking about, but the equivalence stops there.
In C# void is not really a type. It's a keyword that is used to declare return-less methods, but you can't use it in type positions - e.g. you can't have void variables, parameters, type parameters, etc. That's why we have a separate delegate Action instead of just using Func<void>.
The type System.Void is a different animal altogether: it exist only to support reflection, but it's not the same as void, the way System.Int32 is the same as int.
In F#, on the other hand, the type unit is mostly just a regular type, like any other. It even has a definition in the standard library. Yes, the compiler does give it some special treatment in certain special cases, but that's only for optimization and interop, invisible at the language level.
Consequently, unit can be used in any type position - parameters, variables, whatever. This means that F#, for example, doesn't need a special function type for functions that don't return anything, it can just use int -> unit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With