具有特殊左参数的 F# 动态运算符

F# dynamic operator with special left argument

在以下 F# 片段中

let (?) (map : Map<string, string>) (key : string) = map.[key]

let map = [("aKey", "aValue"); ("another Key", "another Value")] |> Map.ofList

let a = map.["aKey"]
let b = map?aKey
assert (a = b)

let c = map.["another Key"] //OK
let d = map?another Key //How should this be written?
assert (c = d)

let d = map?... 行的正确语法是什么?

我想这就是你想要的

let (?) (map : Map<string, string>) (key : string) = map.[key]

let map = [("aKey", "aValue"); ("another Key", "another Value")] |> Map.ofList

let a = map.["aKey"]
let b = map ? ("aKey") 
let b = map?aKey // alternative, as you wrote it
assert (a = b)

let c = map.["another Key"] 
let d = map ? ("another Key") // here the lexer requires " and even brackets
assert (c = d)

F# 有特殊的运算符,使无效的标识符有效。在 docs 中没有太多关于此运算符的信息,甚至连名字都没有

``...``

我会称之为双后背。它主要用于数据访问,例如

// process CSV file with header "Production year,Make,Model"

type Cars = CsvProvider<"file.csv">
let sample = Cars.GetSample()
for row in sample.Rows do
    row.``Production year`` |> printfn "%d"

在您的示例中,您可以使用

访问密钥
let d = map?``another key``

C# 编译器为记录生成 public 方法 <Clone>$。它的名字很奇怪,所以没有人会尝试访问它,但我们可以从 F#

let myRecord = ...;
let copyOfRecords = myRecord.``<Clone>$``()

Roslyn 团队试图故意隐藏此成员,所以不要使用此代码,只知道这是可能的。