F#(.. ..)运算符使用/重载

uhr*_*hrm 5 f# operator-overloading

我有兴趣使用/重载"范围步骤"操作符(.. ..),但我不能为我的生活找到如何使用它.

在文档中说

// Usage:
start .. step .. finish
Run Code Online (Sandbox Code Playgroud)

但是在F#shell中尝试它会产生错误:

> let x = 1 .. 2 .. 7;;

  let x = 1 .. 2 .. 7;;
  ----------^^

stdin(54,11): error FS0010: Unexpected symbol '..' in binding. Expected incomplete structured construct at or before this point or other token.
Run Code Online (Sandbox Code Playgroud)

但是,可以"明确地"调用它:

> let x = (.. ..) 1 2 7;;

val x : seq<int>
Run Code Online (Sandbox Code Playgroud)

是否只能将此运算符用于列表/ seq构造,例如[1..2..7]seq {1..2..7}

Dan*_*iel 1

规范\xc2\xa7 6.3.12对此没有明确说明,但给出的唯一示例是在序列表达式内。这一点,再加上其他方法都不起作用的事实,似乎证实了你的结论。

\n\n

仅供参考 - 以下是范围运算符的相关文档。

\n\n

Operators.( .. )<^T>
\nOperators.( .. .. )<^T,^Step>

\n\n

第 3.8.2 节还提到了 的特殊处理(.. ..),因此可以安全地假设它受到与典型函数/运算符不同的限制/行为的影响。

\n