F# supports a type constraint for \"unmanaged\". This is not the same as a value type constraint like \"struct\" constraints. MSDN notes that the behavior of the unmanaged
I've got some feedback, beware that I don't know F# nearly well enough. Please edit where I goof. Getting to the basics first, the runtime does not actually implement the constraints that F# supports. And supports more than what C# supports. It has just 4 types of constraints:
And the CLI specification then sets specific rules on how these constraints can be valid on a specific type parameter type, broken down by ValueType, Enum, Delegate, Array and any other arbitrary type.
Language designers are free to innovate in their language, as long as they abide by what the runtime can support. They can add arbitrary constraints by themselves, they have a compiler to enforce them. Or arbitrarily choose to not support one that the runtime supports because it doesn't fit their language design.
The F# extensions work fine as long as the generic type is only ever used in F# code. So the F# compiler can enforce it. But it cannot be verified by the runtime and it will not have any effect at all if such a type is consumed by another language. The constraint is encoded in the metadata with F# specific attributes (Core.CompilationMapping attribute), another language compiler knows beans what they are supposed to mean. Readily visible when you use the unmanaged constraint you like in an F# library:
namespace FSharpLibrary
type FSharpType<'T when 'T : unmanaged>() =
class end
Hope I got that right. And used in a C# project:
class Program {
static void Main(string[] args) {
var obj = new Example(); // fine
}
}
class Foo { }
class Example : FSharpLibrary.FSharpType { }
Compiles and executes just fine, the constraint is not actually applied at all. It can't be, the runtime doesn't support it.