As Brian mentions, statically resolved type variables and member constraints won't be of much help here. Maybe one day we'll have typeclasses or something :).

Meanwhile, for this particular case, there's an easy fix:

1
2
3
4
5
let toFloat (v:obj option) =
    match v with
    | None -> failwith ("'widthratio' cannot convert empty value to a numeric")
    | Some value -> try System.Convert.ToDouble(value)
                    with _ -> failwith "not numeric"

> toFloat (Some (box "1.2"));;
val it : float = 1.2

> toFloat (Some (box (System.Exception())));;
Microsoft.FSharp.Core.FailureException: not numeric
at FSI_0008.toFloat(Option`1 v)
at <StartupCode$FSI_0014>.$FSI_0014._main()
stopped due to error

How's that?

By on 3/27/2009 2:28 PM ()

Thanks guys. That certainly works, I think i got a little carried away with doing everything f#, and forgot about good 'ole .net classes :-p

thanks, guys.

By on 3/29/2009 3:41 PM ()

I think the language spec calls these ^a types 'static resolvable variables types'; they come about from 'inline' generic functions with '^' types.

I think you must indeed pattern match out all the types you care about. You can factor this bit out into a function, or create an active pattern, if you find yourself repeatly doing this.

At the end of the day, though, each ^type compiles into different code, and so if you're doing run-time type checks, you'll have to 'manually demux' in the code somewhere in order to get this back into the compile-time-known-statically-type domain.

By on 3/27/2009 2:24 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