Been there, done that:

[link:cs.hubfs.net]

I still wonder if anyone could possibly make it work with explicit conversion operators as well.

By on 6/16/2009 12:18 PM ()

Is where a file should I place this code to in order to use it from any F# project?

By on 6/17/2009 1:32 PM ()

Unfortunately, no. The static type constraint optimizers are only supported in the Microsoft.FSharp.Core library when its compiled.

You could of course define two operators or functions though and use one or the other. You'll get squiggles under the value that does not have an op_Impicit or op_Explicit for the particular type your attempting to convert.

Heres some code that you can use anywhere:

1
2
3
4
let inline (!>) (arg:^b) : ^a = (^b : (static member op_Implicit: ^b -> ^a) arg)

let inline (.!>) (arg:^b) : ^a = (^b : (static member op_Explicit: ^b -> ^a) arg)

thus you can then use it like so:

1
2
let xn : XName = !> "bob" // using op_Implicit

or

1
2
let i: int = .!> 3m // using op_Explicit on System.Decimal
By on 6/17/2009 2:41 PM ()

By the way I discovered a compiler error when attempting to access the op_Explicit on System.Decimal its complaining that there are multiple methods with the same argument types but different return values.

the F# compiler can't pick the right op_Explicit overload by return type appairently when using a constraint.

op_Explicit will work correctly if called explicitly:

1
2
let i : int = System.Decimal.op_Explicit(3m)

*Note - This bug is now fixed in the code base coming up.

By on 6/17/2009 3:06 PM ()

With regard to the bug on accessing the op_Explicit for System.Decimal - thanks - we've fixed this bug in our development tree.

Kind regards

don

By on 6/25/2009 2:33 PM ()

The problem is that they took the static type constraint optimizers out of user code. otherwise you'd be able to write something like this:

let inline (!>) (arg:^b) : ^a =

(^b : (static member op_Implicit: ^b -> ^a) arg)

when ^b : ^b = (^b : (static member op_Explicit : ^a) arg)

but alas they thought it was a good idea to restrict this to the F# library code only.

By on 6/17/2009 12:55 PM ()

I vote for this too. 'conv' would be nice (as it is more readable than a couple of symbols).

Is where any way to implement this in current F# library?

By on 6/12/2009 11:22 PM ()
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