1

我想在结构 a 中声明一个中缀运算符以在结构外使用。但即使结构已打开,我似乎也无法在结构外识别“中缀”。下面是一个使用 Poly/ML 的示例:

> structure A = struct infix 6 ++ fun a ++ b = a + b end;
structure A: sig val ++: int * int -> int end
> 1 A.++ 2;
poly: : error: Type error in function application.
   Function: 1 : int
   Argument: A.++ : int * int -> int
   Reason: Value being applied does not have a function type
Found near 1 A.++ 2
Static Errors
> let open A in 1 ++ 2 end;
poly: : error: Type error in function application.
   Function: 1 : int
   Argument: ++ : int * int -> int
   Reason: Value being applied does not have a function type
Found near let open A in 1 ++ 2 end
Static Errors

这是标准 ML 的限制吗?

4

1 回答 1

1

是的,标准 ML 不支持这一点。每次您open构建该结构时,您都必须重新声明固定性和优先级。解决它的一种方法是全局声明固定性,即在任何结构之外,但是单独编译不能很好地支持这一点,而且它也不是非常模块化。您可以在MLton 的 InfixingOperators 页面上阅读有关它的更多信息以及可能的解决方法。

对于我自己的项目,我在我的文本编辑器中定义了一个快捷方式,它将扩展为一个open声明和一个固定的。

另外,作为个人风格指南,我没有声明优先级。如果我需要将多个中缀运算符混合到同一个表达式中,我宁愿显式地使用括号。将标识符解析为中缀很容易,而解析优先级则不然。

于 2016-03-22T19:32:55.940 回答