在当前版本的Rascal中,周围范围中存在的模式中的变量不匹配和映射,而是检查是否相等。
所以:
<int x, x> := <2,2> => true // x is first introduced and then checked for equality
<int x, x> := <2,3> => false // x is first introduced and then checked for equality
{ int x = 1; if (x := 2) println("true"); else println("false"); // false!
而这对于我们使用模式匹配的所有地方。
我们对这种特殊设计的“非线性匹配”有几个抱怨,我们打算很快添加一个运算符($)来确定从环绕声范围中取出某个东西的意图。如果不使用运营商,那么阴影将出现:
<int x, $x> := <2,2> => true // x is first introduced and then checked for equality
<int x, $x> := <2,3> => false // x is first introduced and then checked for equality
<int x, x> := <2,3> // static error due to illegal shadowing
<int x, y> := <2,3> => true // x and y are both introduced
{ int x = 1; if ($x := 2) println("true"); else println("false"); // false!
{ int x = 1; if (x := 2) println("true <x>"); else println("false"); // true, prints 2! or perhaps a static error.
还可以添加一些额外的动力来获得表达式分成模式为:
<1, ${1 + 2 + 3}> := <1,6> // true
啊,谢谢。期待有更多美元的来源,;) – day
:-)你知道我们为什么用美元作为时间日期文字吗? – jurgenv
呃,没有。我很好奇,:) – day