F#位标志枚举 [英] F# bit flags enum
本文介绍了F#位标志枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在F#中创建一个bitflags枚举,使用位移位运算符以提高可读性:例如
I would like to create a bitflags enum in F#, using bitshift operators for readability: e.g.
[<Flags>]
type DaysOfWeek =
| Monday = 1 <<< 0
| Tuesday = 1 <<< 1
| Wednesday = 1 <<< 2
| Thursday = 1 <<< 3
| Friday = 1 <<< 4
| Saturday = 1 <<< 5
| Sunday = 1 <<< 6
但是,F#编译器对此不满意.它说成员定义中的意外的中缀运算符".
However, the F# compiler dislikes this. It says "unexpected infix operator in member definition".
相对于2的手动幂,我更喜欢这种方法.有办法说服编译器我不是邪恶的人吗?
I would prefer this approach over manual powers of two. Is there a way to convince the compiler that I'm not being evil?
推荐答案
这不是您所要求的,但是您可以使用0b
前缀来编写二进制正写,例如:
It's not exactly what you asked for, but you can write the binary longhand using the 0b
prefix, eg:
[<Flags>]
type DaysOfWeek =
| Monday = 0b0000001
| Tuesday = 0b0000010
| Wednesday = 0b0000100
| Thursday = 0b0001000
| Friday = 0b0010000
| Saturday = 0b0100000
| Sunday = 0b1000000
这篇关于F#位标志枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文