Scala 2.8 behavior changed: _?

Using XScalaWT , this is compiled in Scala 2.7:

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

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

With 2.8.0 RC1, I get this error:

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

Types:

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

So it looks like _ is now attached to an external function instead of an internal one.

Is this change intentional?

UPDATE . The following is an example with a minimum value:

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

Interestingly, this works:

scala> foo(conv(println _))
1
+3
source share
2 answers

Here is the answer I received from Lucas Ritz on the scala -user list:

Hi Aleksey,

there was a change in semantics when we introduced named arguments. Expression

foo(a = _)

:

foo(x => a = x)

2.8 "a" , :

x => foo(a = x)

.

+2

Source: https://habr.com/ru/post/1743299/


All Articles