2013-10-30 44 views
2

我发现有时我必须为模式变量明确指定类型,否则Rascal将无法按预期工作。控制台中的以下会话说明了这一切:模式变量的类型

rascal>data foo = bar(int); 
ok 

rascal>int x = 1; 
int: 1 

rascal>[x | bar(x) <- [bar(2), bar(3)]]; 
list[void]: [] 

rascal>[x | bar(int x) <- [bar(2), bar(3)]]; 
list[int]: [2,3] 

为什么会发生这种情况?

回答

3

在当前版本的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 
+0

啊,谢谢。期待有更多美元的来源,;) – day

+0

:-)你知道我们为什么用美元作为时间日期文字吗? – jurgenv

+0

呃,没有。我很好奇,:) – day