Domanda

I've been playing with MS Solver using F# and I need to define domain as a range of ints. I learnt there's a function: Domain.IntegerRange which takes two parameters of Rational type.

In C# there's no problem using ints instead of Rationals: http://msdn.microsoft.com/en-us/library/ff826356(v=vs.93).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

However, in F# such an implicit conversion doesn't get accepted. How should that be implemented instead ? I've tried to create somehow Rational based on an int, but failed to do so.

È stato utile?

Soluzione

The Rational class supports implicit conversions from int, float, etc. and this is seamless in C#. F#, on the other hand, does not do implicit conversions without you asking for them... well... explicity.

You can invoke the op_Implicit operator directly to get what you want:

let rat (i:int) = Rational.op_Implicit(i)
Domain.IntegerRange(rat 6, rat 8)

It's also possible to define an operator to do this in general, see the standard approach in Tomas's answer to this question.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top