I’ve written the following
[<Measure>]
type m
[<Measure>]
type s
[<Measure>]
type v = m/s
type Vector3<[<Measure>] 'a> =
{
X : float<'a>
Y : float<'a>
Z : float<'a>
}
static member (*)
(v:Vector3<'a>,f:float<'b>):Vector3<'a*'b> =
{ X = v.X*f; Y = v.Y*f ; Z = v.Z * f}
Now I’m trying to use it this way:
let next_pos (position:Vector3<m> , velocity: Vector3<m/s> ,dt : float<s> -> Vector3<m>) =
position + (velocity * dt)
It gives me a compiler error, but I’m pretty sure the measure unit are expressed right.
What’s my mistake?
The syntax you tried to use for specifying the return type was incorrect. It should look like this:
To specify that the function returns a value of type
Vector3<m>, you need to add type annotation to the result, which is done by writinglet foo <arguments> : T = <expr>. When adding type annotations to parameters, these need to be parenthesized (so the syntax is not ambiguous). As noted by Paolo in a comment, your use of->was saying thatdtis a function, because the annotationfloat<s> -> Vector3<m>was attached to the parameterdt.To make the code compile, I also had to add an implementation of
(+)operator to yourVector3, but I assume you have that already (and just left it out when posting the question).