将Scala单位转换为Java Void [英] Convert Scala Unit to Java Void
问题描述
我有一个采用Function[String, Void]
的Java类,该类类似于Consumer
功能接口.我正在从Scala代码中调用此Java函数.如下所示,我接受Scala上的function: => Unit
,因为它等效于void.当我尝试应用此功能代替Void
返回类型时,会发生类型不匹配的情况. .asInstanceOf[Void]
的显式强制转换也引发了异常.如何将=> Unit
转换为=> Void
?
I have a Java class which takes Function[String, Void]
, which is similar to Consumer
functional interface. I am calling this Java function from Scala code. As you can see below, I accept function: => Unit
on Scala side as it is void equivalent. When I attempt to apply this function in place of Void
return type type mismatch happens. Explicit casting of .asInstanceOf[Void]
also threw an exception. How do we convert => Unit
to => Void
?
// Java
class AcceptFunction {
public void temp(Function<String, Void> f) {
f.apply("Hello");
}
}
// Scala
def temp(f: String => Unit): Unit = {
new AcceptFunction().temp(new Function[String, Void]() {
override def apply(t: String): Void = f(t) // <===== error expecting Void instead of Unit
})
}
推荐答案
显然,
scala> new jfunc.Acceptor().f(s => println(s))
<console>:12: error: type mismatch;
found : Unit
required: Void
new jfunc.Acceptor().f(s => println(s))
^
scala> new jfunc.Acceptor().f { s => println(s) ; null }
hello, world.
您显示的Java所在的位置:
where the java is like you show:
package jfunc;
import java.util.function.*;
public class Acceptor {
public void f(Function<String, Void> g) { g.apply("hello, world."); }
}
可以:
scala> :javap -c -
public static final java.lang.Void $anonfun$res0$1(java.lang.String);
Code:
0: getstatic #33 // Field scala/Predef$.MODULE$:Lscala/Predef$;
3: aload_0
4: invokevirtual #37 // Method scala/Predef$.println:(Ljava/lang/Object;)V
7: aconst_null
8: areturn
public $line3.$read$$iw$$iw$();
Code:
0: aload_0
1: invokespecial #39 // Method java/lang/Object."<init>":()V
4: aload_0
5: putstatic #41 // Field MODULE$:L$line3/$read$$iw$$iw$;
8: aload_0
9: new #43 // class jfunc/Acceptor
12: dup
13: invokespecial #44 // Method jfunc/Acceptor."<init>":()V
16: invokedynamic #63, 0 // InvokeDynamic #0:apply:()Ljava/util/function/Function;
21: invokevirtual #67 // Method jfunc/Acceptor.f:(Ljava/util/function/Function;)V
24: getstatic #72 // Field scala/runtime/BoxedUnit.UNIT:Lscala/runtime/BoxedUnit;
27: putfield #74 // Field res0:Lscala/runtime/BoxedUnit;
30: return
这篇关于将Scala单位转换为Java Void的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!