Reputation: 10885
For a small AST parser i have a small discriminated union
type Numerical =
| Int of int
| Real of float
for the use in some other constructs like
type Vector = Numerical list
Vector [Int 42; Real 13.5]
When i have a method like this
let eval (e:Numerical) =
match e with
| Int n -> ... (* return int *)
| Real r -> ... (* return float *)
I know that F# infers the int
type and produces an error with the Real
pattern in the second line, so I want to know, which code-design would be the best one to be able to handle such "generic" types and return their appropriate values with the given types.
EDIT
I have here an either or situation which leads to functions like
let getInt = function
| Int n -> n
| Real _ -> failwith "Given value doesn't represent an int"
let getReal = function
| Real n -> n
| Int _ -> failwith "Given value doesn't represent a real number"
however i would like a method which encapsulates both cases and "automatically chooses the correct one".
This whole effort should lead to the possibility to be able to really run computations with the "boxed" values like Int 42
and Real 13.
with primitive data types but to able to return the appropriate wrapper. If i want to add Real 1.
and Real 1.5
I want to extract 1.0 + 1.5 = 2.5
and then go on with Real 2.5
, however I do not want to handle everything as floats so that I have the distinction between ints and floats.
Upvotes: 0
Views: 290
Reputation: 40145
let eval<'T> (e:Numerical):'T =
match e with
| Int n -> n :> obj :?> 'T
| Real r -> r :> obj :?> 'T
DEMO
> eval<float>(Real 4.5);;
val it : float = 4.5
> eval<int>(Int 42);;
val it : int = 42
> let x:float = eval (Real 5.5);;
val x : float = 5.5
> let x:int = eval (Real 5.5);;//NG typemismatch
Upvotes: 0
Reputation: 244777
You could cast the results to obj
:
let eval (e:Numerical) =
match e with
| Int n -> n :> obj
| Real r -> r :> obj
But that's probably not what you want.
Another option would be implement your own operations on Numerical
:
let (+) a b =
match (a,b) with
| (Int an, Int bn) -> Int (an + bn)
| (Real ar, Real br) -> Real (ar + br)
| _ -> failwith "Can't add Int and Real"
You can't have a function that has different compile-time return type based on some run-time value.
Upvotes: 3