Reputation: 4062
type floatType = float32
let a = 5.0
let b = float32 a // Works
let b' = floatType b // Does not work
Would it be possible to cast to a type abbreviation?
Upvotes: 1
Views: 384
Reputation: 26174
Yes, it's possible to use a type alias for casting, but you're not casting, that's not the syntax for casting. In your example you're using an explicit conversion.
Casting is a different thing, it "converts" to a super-class (up-casting) or a sub-class (down-casting), see this modification of your example:
type floatType = float32
let a = box 5.0f
let b = a :?> float32
let b' = a :?> floatType
This example is down-casting and as you can see it works with the alias.
So it's not possible to cast from float
to float32
, even without type annotations.
Upvotes: 4
Reputation: 6223
When writing float32 a
, you are using the function Microsoft.FSharp.Core.Operators.float32
, which does an explicit conversion to a single-precision float without units.
If you want to allow similar usage of floatType
, you could add the following to your definitions:
let inline floatType a = float32 a
Now your code should work.
Upvotes: 2