For example I have the following code:
type feature_mask =
| FEATURE1 [@value 1]
| FEATURE2 [@value 8]
| FEATURE3 [@value 0x400]
[@@deriving enum, show]
let decode_feature x =
let feature1 = if x land 1 > 0 then "FEATURE1" else "" in
let feature2 = if x land 8 > 0 then "FEATURE2" else "" in
let feature3 = if x land 0x400 > 0 then "FEATURE3" else "" in
String.concat ~sep:" | " [feature1; feature2; feature3]
But the code is ugly in a way that I have do use the numbers in two places, instead of defining them only once. Also it looks a bit like spaghetti. Are there any ways to make it “the right way”?
I filled an issue in ppx_deriving for adding bitmasks, but wonder if there is a way to do that without patching ppx_deriving yet.