I looked at the source code for the Option type, in prim-types.fs, and found they define Option.IsNone and IsSome with pattern matching - so there probably is no shortcut.

You might want to declare a member function...

1
2
3
4
5
6
7
8
9
10
type T = 
    | Simple | Complex of int
    member x.IsComplex = match x with |Simple -> false | Complex n -> true

let x = Simple

if x.IsComplex then
    printfn "Complex"
else
    printfn "Simple"
By on 11/20/2009 5:16 PM ()

An if..then expression IS a pattern match; its just been shortened up with some syntax sugar. So what's bugging you about the match expression?

Also, your pattern is incorrect. It should be

if x = Complex(v) then

...

else

...

Of course, if you do that, the compiler is going to moan about v being undefined. So, your options are:

1) define v with something like let v = 42

2) replace v with an actual integer (i.e. 42)

3) use a match expression (which "feels" like the right answer to me)

For example,

match x with

| Complex(v) -> ...

| Simple -> ...

By on 11/16/2009 7:15 AM ()
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