The first match works, but not the second one. Is there any way to match without declaring variables, beside using a chain of if/elif ?
(Note that I use the value elem, while I match the variable t)
let t = typeof<string>
match propType with
| t -> elem.GetValueAsString() :> obj
| typeof<string> -> elem.GetValueAsString() :> obj
Your first pattern actually doesn't match typeof<string>
. It binds propType
to a new value t
shadowing the previous t
which is equals to typeof<string>
.
Since typeof<string>
is not a literal, the second pattern doesn't work as well (although it is a redundant pattern in your example). You have to use when
guard as follows:
match propType with
| t when t = typeof<string> -> elem.GetValueAsString() :> obj
| t -> elem.GetValueAsString() :> obj
If you're trying to match against the type of a value, you can use the :? operator
Example :
let testMatch (toMatch:obj) = match toMatch with
| :? string as s -> s.Split([|';'|]).[0]
| :? int as i -> (i+1).ToString()
| _ -> String.Empty
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With