I am using OpenTK with its math library but unfortunately there is no generic interface for the vector classes. For example Vector2 ,3 and 4 all have the same static method SizeInBytes http://www.opentk.com/files/doc/struct_open_t_k_1_1_vector3.html#ae7cbee02af524095ee72226d842c6892
Now I could just overload tons of different constructors but I think it should be possible to solve this via type constraints.
I was reading though http://msdn.microsoft.com/en-us/library/dd233203.aspx and I found this
type Class4<'T when 'T : (static member staticMethod1 : unit -> 'T) > =
class end
Now I have tried it by myself but I can't get the syntax right.
type Foo<'T when 'T: (static member SizeInBytes: unit -> int)>(data: 'T []) =
member this.GetBytes() = 'T.SizeInBytes()
let f = Foo([|new Vector3(1.0f,1.0f,1.0f)|])
f.GetBytes()
Can you spot the problem?
Edit:
VS2012 complains about this line 'T.SizeInBytes() //Unexpected symbol or expression and T.SizeInBytes() doesn't work either.
Edit2:
I made an example that doesn't involve an external library
type Bar() =
static member Print() = printf "Hello Foo"
type Foo<'T when 'T: (static member Print: unit -> unit)>(data: 'T []) =
member this.Print() = 'T.Print()
let b1 = Bar()
let f = Foo([|b1|])
f.Print()