Ramy
Ramy

Reputation: 21261

cast literal to byte in F#

Here is my code snip:

let targetMeasure (usesPercent:Nullable<byte>) value =
            match usesPercent.Value with
            | 0 -> sprintf "$%A" value
            | _ -> sprintf "%A%%" value

Trying to match "usesPercent" with 0, but 0 needs to be a byte. This has always confused me in F#. I get past the nullable stuff, but then the values aren't of the same type. How do I get around this?

Upvotes: 0

Views: 570

Answers (2)

desco
desco

Reputation: 16782

let targetMeasure (usesPercent:Nullable<byte>) value =
    match usesPercent.Value with
    | 0uy -> sprintf "$%A" value
    | _ -> sprintf "%A%%" value

Upvotes: 3

Tomas Petricek
Tomas Petricek

Reputation: 243041

You can create byte literal by writing 0uy. A (pretty) complete list of numeric literals is printed by F# Interactive if you write some that is invalid (e.g. 0z). Then you get:

Sample formats include 4, 0x4, 0b0100, 4L, 4UL, 4u, 4s, 4us, 4y, 4uy, 4.0, 4.0f, 4I.

For integers, it is quite easy to decode u stands for unsigned and y, s, (nothing), L stand for 8bit, 16bit, 32bit and 64bit respectively.

Upvotes: 6

Related Questions