In C# I can do:
var castValue = inputValue as Type1 In F#, I can do:
let staticValue = inputValue :> Type1 let dynamicValue = inputValue :?> Type1 But neither of them is the equivalent of the C#'s keyword as.
I guess I need to do a match expression for the equivalent in F#
match inputValue with | :? Type1 as type1Value -> type1Value | _ -> null Is this correct?
As far as I know, F# doesn't have any built-in operator equivalent to C# as so you need to write some more complicated expression. Alternatively to your code using match, you could also use if, because the operator :? can be use in the same way as is in C#:
let res = if (inputValue :? Type1) then inputValue :?> Type1 else null You can of course write a function to encapsulate this behavior (by writing a simple generic function that takes an Object and casts it to the specified generic type parameter):
let castAs<'T when 'T : null> (o:obj) = match o with | :? 'T as res -> res | _ -> null This implementation returns null, so it requires that the type parameter has null as a proper value (alternatively, you could use Unchecked.defaultof<'T>, which is equivalent to default(T) in C#). Now you can write just:
let res = castAs<Type1>(inputValue)
I would use an active pattern. Here is the one I use:
let (|As|_|) (p:'T) : 'U option = let p = p :> obj if p :? 'U then Some (p :?> 'U) else None Here is a sample usage of As:
let handleType x = match x with | As (x:int) -> printfn "is integer: %d" x | As (s:string) -> printfn "is string: %s" s | _ -> printfn "Is neither integer nor string" // test 'handleType' handleType 1 handleType "tahir" handleType 2. let stringAsObj = "tahir" :> obj handleType stringAsObj
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