In F# can I represent a restricted type without defining a class? Lets say I want to represent all the pairs of number where the first divides the second.
in C# I could do:
class PairDivides
{
int a {get;private set;}
int b {get;private set;}
PairDivides(int first, int second)
{
if (a % b != 0)
{
throw new Exception();
}
this.a = first;
this.b = second;
}
}
Now it's impossible to create a PairDivides
instance where b
doesn't divide a
...
Can this be done in F# using only the functional constructs (record, discriminated union, maybe active patterns, etc.)?
I want to be able to create and receive something like these pairs being certain that they are constructed correctly.