Closed3
F#でビットフラグ
なんかすぐ忘れそうなのでメモ
open System
[<Flags>]
type DeathFlag =
| MarrigeWhenWarIsOver = 1
| JoinLater = 2
| CapcomHelicopter = 4
こんなふうに[<Flags>]
属性をつける
enum <-> int の変換はこんな感じ
open System
[<Flags>]
type DeathFlag =
| MarrigeWhenWarIsOver = 1
| JoinLater = 2
| CapcomHelicopter = 4
let bobsFlags = DeathFlag.MarrigeWhenWarIsOver ||| DeathFlag.JoinLater
let bobsFlagsToInt = int bobsFlags
// val bobsFlagsToInt: int = 3
let bobsFlagsFromInt = enum<DeathFlag> bobsFlagsToInt
// val bobsFlagsFromInt: DeathFlag = MarrigeWhenWarIsOver, JoinLater
bobsFlags = bobsFlagsFromInt
// val it: bool = true
Arrayにする
open System
[<Flags>]
type DeathFlag =
| MarrigeWhenWarIsOver = 1
| JoinLater = 2
| CapcomHelicopter = 4
let bobsFlags = DeathFlag.MarrigeWhenWarIsOver ||| DeathFlag.JoinLater
Enum.GetValues<DeathFlag>()
|> Array.where bobsFlags.HasFlag
// val it: DeathFlag array = [|MarrigeWhenWarIsOver; JoinLater|]
ボブには強く生きて欲しい
このスクラップは2024/06/04にクローズされました