Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to type cast in F#?

Tags:

I have to enumerate through the members of a collection and create an array with a particular property of the members:

  let ops: int array = [| for x in app.Operations ->                             let op=  x : IAzOperation                             op.OperationID |]  

Here app.Operations is a collection of IAzOperation but on enumeration, returns each member as Obj. So I want to type cast each member and access the property. but not sure how to typecast it. If I typecast the way I've mentioned here, it gives me error that:

This espression was expected to have type IAzOPeration but here has type obj. 

What am I missing here?

like image 537
Kapil Avatar asked Mar 27 '12 13:03

Kapil


People also ask

How do you type a cast in F#?

F# Type Casting (Upcasting, and Downcasting) Example F# allows us to cast object of one type to another type by using :> operator. This operator is used to upcast object. we can downcast object by using :?> operator.

How do you type a cast?

Typecasting is making a variable of one type, such as an int, act like another type, a char, for one single operation. To typecast something, simply put the type of variable you want the actual variable to act as inside parentheses in front of the actual variable. (char)a will make 'a' function as a char.

What is type cast operator?

The type cast operator converts the data type of expr to the type specified by type-name : [ type-name ] expr. Explicit type conversions require the type cast operator. Implicit type conversions are performed automatically by 4Test and do not require explicit type casting.

How does type casting work in C?

Type Casting is basically a process in C in which we change a variable belonging to one data type to another one. In type casting, the compiler automatically changes one data type to another one depending on what we want the program to do.


2 Answers

You need the downcasting operator :?>:

let ops: int array = [| for x in app.Operations do                           let op =  x :?> IAzOperation                           yield op.OperationID |]  

As the symbol ? in its name denotes, downcasting could fail and result in a runtime exception.

In case of sequences, you have another option to use Seq.cast:

let ops: int array =      [| for op in app.Operations |> Seq.cast<IAzOperation> -> op.OperationID |]  
like image 66
pad Avatar answered Sep 27 '22 21:09

pad


type Base1() =     abstract member F : unit -> unit     default u.F() =      printfn "F Base1"  type Derived1() =     inherit Base1()     override u.F() =       printfn "F Derived1"   let d1 : Derived1 = Derived1()  // Upcast to Base1. let base1 = d1 :> Base1  // This might throw an exception, unless // you are sure that base1 is really a Derived1 object, as // is the case here. let derived1 = base1 :?> Derived1  // If you cannot be sure that b1 is a Derived1 object, // use a type test, as follows: let downcastBase1 (b1 : Base1) =    match b1 with    | :? Derived1 as derived1 -> derived1.F()    | _ -> ()  downcastBase1 base1 
like image 44
SmartestVEGA Avatar answered Sep 27 '22 21:09

SmartestVEGA