F# 使用 .[] 运算符作为函数
F# using the .[] operator as a function
F# 允许将大多数运算符视为函数。例如:
let f = (+)
f 1 2 // <- 3
但是,我不确定是否可以使用索引运算符 (.[...]
) 使用相同的语法。
我试过:
let f = (.[])
f [| 1 |] 0
None of the types 'int [], int' support the operator '.[]' (using external F# compiler)
并且:
let f = (.[])
f 0 [| 1 |]
None of the types 'int, 'a []' support the operator '.[]' (using external F# compiler)
我的猜测是 .[...]
实际上不是运算符,而是一种特殊的内置语法,不能以功能方式使用,但也许我遗漏了一些东西。是否可以像通常使用 Array.get
?
这样的函数一样使用运算符索引
我没有很好的答案,但我确实有一个可能有用的观察结果。当您尝试定义如下内容时:
let (.[,]) v1 v2 = v1 + v2
您收到一条错误消息:
error FS0035: This construct is deprecated: The treatment of this operator is now handled directly by the F# compiler and its meaning cannot be redefined
我认为过去是使用实际运算符进行索引的情况(我似乎记得可能是这种情况,但谷歌搜索是不可能的),但正如错误消息所暗示的那样,这可能是现在做得不同了。不过,我不确定为什么这不适用于 .[]
!
这些funky operator names appear in a couple of places in the F# compiler source code.
F# 允许将大多数运算符视为函数。例如:
let f = (+)
f 1 2 // <- 3
但是,我不确定是否可以使用索引运算符 (.[...]
) 使用相同的语法。
我试过:
let f = (.[])
f [| 1 |] 0
None of the types 'int [], int' support the operator '.[]' (using external F# compiler)
并且:
let f = (.[])
f 0 [| 1 |]
None of the types 'int, 'a []' support the operator '.[]' (using external F# compiler)
我的猜测是 .[...]
实际上不是运算符,而是一种特殊的内置语法,不能以功能方式使用,但也许我遗漏了一些东西。是否可以像通常使用 Array.get
?
我没有很好的答案,但我确实有一个可能有用的观察结果。当您尝试定义如下内容时:
let (.[,]) v1 v2 = v1 + v2
您收到一条错误消息:
error FS0035: This construct is deprecated: The treatment of this operator is now handled directly by the F# compiler and its meaning cannot be redefined
我认为过去是使用实际运算符进行索引的情况(我似乎记得可能是这种情况,但谷歌搜索是不可能的),但正如错误消息所暗示的那样,这可能是现在做得不同了。不过,我不确定为什么这不适用于 .[]
!
这些funky operator names appear in a couple of places in the F# compiler source code.