gpt4 book ai didi

java - Scala 2.12 和 Java 8 SAM 互操作不编译

转载 作者:搜寻专家 更新时间:2023-11-01 02:40:14 26 4
gpt4 key购买 nike

我正在尝试使用 Scala 测试中的 rx.Observable 测试 Java 8 类。根据 Scala 2.12.0-M3 release notes :

LAMBDA SYNTAX FOR SAM TYPES (EXPERIMENTAL) As of M3, this feature is not yet on by default. You can enable it with the -Xexperimental compiler option.

When the option is enabled, then similar to Java 8, Scala 2.12 allows instantiating any type with one single abstract method by passing a lambda.

但是,使用 Gradle 和 IntelliJ,我无法编译以下内容:

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'
}

最佳答案

scheduler.createWorker().schedule(_ => o.foreach { }) // The argument is an Action0, which has a SAM void call()

这将尝试传递一个带有一个参数的“函数”(而不是一个带有 1 个参数 SAM 的接口(interface)/特征的匿名实例)。 _ 只是表示您不在正文中使用该参数。 Action0call() 接受 0 个参数,因此尝试使用 () => o.foreach { } 代替。

关于java - Scala 2.12 和 Java 8 SAM 互操作不编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35263153/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com