type number = A of int | B of float;;
let plus x1:number x2:number = (number x1) + (number x2);;
The problem you have here is twofold:
You can't use discriminated union cases which have a single value (in this instance, A and B) as though you had the value instead. Think of each case as a class which has one value in it (this is how it is represented in the bytecode anyway). You would need to take the value out of the class before attempting to use it.
F# doesn't allow implicit casting. This means that once you get the values out of your cases, you will need to convert them to a common type. In this case you will need to either cast the
A
value to afloat
or theB
value to anint
.
As @JohnPalmer notes in his answer, the most clear way of addressing these two issues is to use pattern matching.