F# Bitwise operators and units of measure

100 Views Asked by At

When I define a unit of measure in my code, I can use the basic arithmetic operators on values with the unit without any problems, e.g.

type [<Measure>] m

let a = 1<m>
let b = 2<m>
(* no problem *)
let result = a + b

However, I can't use bitwise operators:

type [<Measure>] m

let a = 1<m>
let b = 2<m>
(* error: The type 'int<m>' does not match the type 'int' *)
let result = a &&& b

Is there a way to make the bitwise operators compatible with units of measure?

1

There are 1 best solutions below

2
Michael Samoilenko On

F# units of measure are not directly compatible with bitwise operators like &&& (bitwise AND), ||| (bitwise OR), and ^^^ (bitwise XOR). The reason for this is that units of measure are a static type-checking feature that's primarily used for safety and correctness in numerical computations, and bitwise operations are not supported out of the box for these types.

Related Questions in F#