How to define type synonyms of int, float etc. for various roles in F#?

Viewed 169

I would like to use simple types like 'int' in various roles in a way that the compiler would prevent me to mix up variables of different roles inadvertently. Using units of measure seems to offer a (light) way to solve it.

[<Measure>]
type xcoordinate
[<Measure>]
type ycoordinate

type xci = int<xcoordinate>
type yci = int<ycoordinate>

let fnc (x:xci) (y:yci) = ...

// Now 'fnc' can be called only with a proper x-y coordinate pair.
// Is there any way to use the type synonyms to coerce an 'int' to  
// int<xcoordinate> instead of writing 2<xcoordinate>, for instance? 
1 Answers
Related