Hi, the F# specification says the following about the "as" pattern:

The pattern pat as ident binds the given name to the input value and also matches the value against the given pattern.The pattern pat as ident binds the given name to the input value and also matches the value against the given pattern.

So, you can use it only for assigning identifier to the matched value. This is probably because the typical usage is a bit different - for example:

1
2
let foo = function
  | (x::xs) as list -> ...

If you want to do something like this very often and using nested pattern matching blocks would be uncomfortable, then you can define an active pattern that encapsulates the type conversion functionality and allows you to nest the patterns:

1
2
3
4
5
6
7
8
9
10
11
12
 
// Dynamically casts the input value to any specified type 
let (|DynamicCast|_|) (a:'a) : option<'b> = 
  match box a with 
  | :? 'b as res -> Some(res)
  | _ -> None

let temp = (1, 2)  

// Note: we need type annotations somewhere, to specify the target type!
match temp with
| DynamicCast (a:int, b:int) -> printfn "%A, %A" a b
By on 1/15/2010 5:52 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