2010-04-29 78 views
3

使用XScalaWT,這斯卡拉2.7下編譯:斯卡拉2.8:行爲改變了嗎?

class NodeView(parent: Composite) extends Composite(parent) { 
    var nodeName: Label = null 

    this.contains(
    label(
     nodeName = _ 
    ) 
) 
} 

隨着2.8.0 RC1,我得到這個錯誤:

type mismatch; found : main.scala.NodeView required: org.eclipse.swt.widgets.Label

的類型是:

label(setups: (Label => Unit)*)(parent: Composite) : Label 
contains(setups: (W => Unit)*) : W 

所以看起來像_現在綁定到外部函數而不是內部。

這種改變是故意的嗎?

UPDATE:這是一個最小化的例子:

的Scala 2.7.7:

scala> var i = 0 
i: Int = 0 

scala> def conv(f: Int => Unit) = if (_:Boolean) f(1) else f(0)  
conv: ((Int) => Unit)(Boolean) => Unit 

scala> def foo(g: Boolean => Unit) { g(true) }  
foo: ((Boolean) => Unit)Unit 

scala> foo(conv(i = _))  

scala> i  
res4: Int = 1 

的Scala 2.8.0RC3:

scala> var i = 0 
i: Int = 0 

scala> def conv(f: Int => Unit) = if (_:Boolean) f(1) else f(0) 
conv: (f: (Int) => Unit)(Boolean) => Unit 

scala> def foo(g: Boolean => Unit) { g(true) } 
foo: (g: (Boolean) => Unit)Unit 

scala> foo(conv(i = _)) 
<console>:9: error: type mismatch; 
found : Boolean 
required: Int 
     foo(conv(i = _)) 
       ^

scala> foo(conv(j => i = j)) 

scala> i 
res3: Int = 1 

有趣的是,這個作品:

scala> foo(conv(println _)) 
1 
+0

你能發佈更多或錯誤信息? – 2010-04-29 07:54:21

+0

我沒有發佈錯誤消息:「type mismatch; found:main.scala.NodeView required:org.eclipse.swt.widgets.Label」 – 2010-04-29 08:06:53

+0

編譯器引用哪一行? – 2010-04-29 09:25:38

回答

2

下面是我從盧卡斯Rytz得到了斯卡拉用戶列表上的答案:

Hi Alexey,

there has been a change in semantics when we introduced named arguments. An expression

foo(a = _) 

used to be parsed as follows:

foo(x => a = x) 

In 2.8, "a" is treated as a named argument, i.e. the expression is parsed as:

x => foo(a = x) 

I will add a migration warning for this change.

+1

在某些情況下,您可能會用花括號代替舊的行爲。 – Debilski 2010-06-08 19:42:06

0

我發現同樣的事情,並要求在Dave的博客:

http://www.coconut-palm-software.com/the_new_visual_editor/doku.php?id=blog:simplifying_swt_with_scala#comment__930ba2f0a020203873d33decce01ebc2

沒有回答有尚未雖然。就像你所說的那樣,它似乎像綁定到外部封閉而不是內部封閉。

更改

nodeName = _ 

x => nodeName = x 

解決了這個問題。

+0

是的,但是對我來說XScalaWT代碼的可讀性有點不好。 – 2010-06-04 17:04:05

+0

是的,當然,_的語義發生了巨大變化,這真的很奇怪。我真的很想了解發生了什麼。 – 2010-06-05 00:07:19

+0

如果你還沒有看到答案,它不是_改變的語義,而是=。 – 2010-06-09 05:34:22