Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple arity static type constraint

Let's say I have a bunch of vector types (a la XNA) and some of them have static member Cross:

type Vector3 =
  ...
  static member Cross (a : Vector3, b : Vector3) = new Vector3(...)

I can define the cross function and it compiles:

let inline cross (x : ^T) (y : ^T) = (^T : (static member Cross : (^T * ^T) -> ^T) ((x,y)))

Unfortunately I'm not able to use it and have following error:

let res = cross a b
                 ^

The member or object constructor Cross takes 2 argument(s) but is here given 1. The required signature is static member Vector3.Cross : a:Vector3 * b:Vector3 -> Vector3

Is it even possible at all? Thanks for helping!

like image 515
Stringer Avatar asked Nov 11 '10 14:11

Stringer


1 Answers

You've over-parenthesized your static member signature. Try this instead:

let inline cross (x : ^T) (y : ^T) = 
  (^T : (static member Cross : ^T * ^T -> ^T) (x,y))

Given your definition, F# was looking for a member Cross which takes a single argument of tuple type.

like image 125
kvb Avatar answered Dec 02 '22 14:12

kvb