Interesting, it does not seem possible ('as' only accepts an identifier, not a pattern). Here is best I could do:

1
2
3
4
5
6
7
8
9
10
 

let o = box (1,"foo")

match o with

| :? (int * string) as t when (match t with x,y -> x > 3) -> match t with x,y -> printfn "%d,%s" x y

| _ -> printfn "either not the right tuple type, or was but x <= 3"

but it matches 't' twice.

By on 5/14/2009 1:23 AM ()

Hi,

It feels like there should be a better way to do this but, this would be my first thought:

1
2
3
4
5
6
7
8
9
 

let matchTupleWithBinding(o : obj) = 
    match o with 
    | :? (string*string) when (let url,_ = (o :?> (string*string)) in url.Length < 10) ->         
        let _,content = o :?> (string*string)
        content
    | _ -> "Unknown"

As I said, I suspect there may be a way to avoid performing the cast twice. I'll come back if anything springs to mind.

Regards,

Michael

By on 5/14/2009 12:32 AM ()

Or even:

1
2
3
4
5
let matchTupleWithBinding2(o : obj) = 
    match o with 
    | :? (string*string) as s when (fst s).Length < 10        ->
        snd s
    | _ -> "Unknown"

Regards,

Michael

By on 5/14/2009 12:38 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