Classes require explicit type parameters. It works:
type Point2D<'T>(x:'T, y:'T) = member this.X = x member this.Y = y let float32Point = Point2D(0.0f, 1.0f) let intPoint = Point2D(0, 1)
To answer your second question, your definition of min
has the signature 'a -> 'a -> 'a (requires comparison)
. The comparison
constraint exists only at compile time (the runtime signature is the same, minus the constraint).
<
is replaced by a call to GenericLessThanIntrinsic
, which has a constraint. The restriction applies only to callers.
In addition, from section 14.6.7 of the specification:
Generalization is the process of defining a typical type to determine where possible, which makes the construct reusable with several different types. Generalization is used by default in all definitions of functions, values, and members , except as noted below in this section. Generalization also applies to member definitions that implement common virtual methods in object expressions.
(in italics)
Note that classes are not listed. I believe that this does not give a reason, but it is by design.
source share