Scala 2.12 and Java 8 SAM interop not compiling

I am trying to test the Java 8 class using rx.Observablefrom Scala Test. According to Scala 2.12.0-M3 release notes :

LAMBDA SYNTAX FOR SAM TYPES (EXPERIMENTAL) With M3, this feature is not yet enabled by default. You can enable it using the -Xexperimentalcompiler.

When this option is enabled, similarly to Java 8, Scala 2.12 allows creating an instance of any type using one abstract method by passing a lambda.

However, using Gradle and IntelliJ, I cannot get the following for compilation:

val o: rx.Observable[util.Map.Entry[String, _ <: util.Collection[String]]] = ???
val scheduler = new TestScheduler()
scheduler.createWorker().schedule(_ => o.foreach { }) // The argument is an Action0, which has a SAM void call()

build.gradle

apply plugin: 'scala'

group = 'name.abhijitsarkar.scala'
version = '1.0-SNAPSHOT'

tasks.withType(ScalaCompile) {
    scalaCompileOptions.useAnt = false
    scalaCompileOptions.additionalParameters = ["-feature", "-Xexperimental"]
    targetCompatibility = "1.8"
}

dependencies {
    compile 'org.scala-lang:scala-library:2.12.0-M3'
    compile 'com.typesafe.akka:akka-stream-experimental_2.11:2.0.3'
    compile 'io.reactivex:rxjava:1.1.0'
    testCompile 'org.scalatest:scalatest_2.12.0-M3:3.0.0-M12'
}
+4
source share
1 answer
scheduler.createWorker().schedule(_ => o.foreach { }) // The argument is an Action0, which has a SAM void call()

"" (, / 1- SAM). _ , . Action0 call() 0 , () => o.foreach { }.

+4

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


All Articles