Hi,

This feature is mostly used internally, in the standard library (look at FSharp.Core/prim-types.fs for examples). It's the way to get member constraints for operators like +. The (+) operator requires its arguments to have "static member (+)" method.

Member constraints are quickly described in the specs: [link:research.microsoft.com]

^pos is a statically resolved variable type (it's not really generic, because the compiler needs to know the exact type).

Laurent.

By on 9/24/2008 1:10 PM ()

Thanks for the answer, but my question was more about the mismatch between ^pos and ^vec. The parameters to the function were declared to be of type ^pos, but the constraint applied on type ^vec.
I was surprised that the compiler did not complain about me constraining a fresh type variable.

By on 9/27/2008 1:24 AM ()

Hi,

Ah, OK. Your (*) operator works with any ^vec type that has a scale member. The compiler detects that ^pos and ^vec are the same, and that ^pos type requires a scale method.

If you remove some type annotation, i.e.

let inline drag k pos speed =

let (*) k x = (^vec: (static member inline scale: float -> ^vec -> ^vec) k) x

-k * speed

Then, type inference tells that speed has ^vec type.

When you use type variables, you can't say two type variables must be different. For example, this code is valid:

let f (x: 'a) (y: 'b) = x = y

The compiler tells that x and y have the same type.

Laurent

By on 9/27/2008 2:07 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper