def ensure(f: => Unit): Unit
}
scala> abstract class A {
| def ensure(f: => Unit): Unit
| }
defined class A
scala> :javap -p A
Compiled from "<console>"
public abstract class A {
public abstract void ensure(scala.Function0<scala.runtime.BoxedUnit>);
public A();
}a.ensure(new scala.Function0<scala.runtime.BoxedUnit>{
public scala.runtime.BoxedUnit apply() {
//do some stuff
return scala.runtime.BoxedUnit.UNIT
}
})--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
scala> :javap -p scala.Function0
Compiled from "Function0.scala"
public interface scala.Function0<R> {
public abstract R apply();
public abstract java.lang.String toString();
public abstract boolean apply$mcZ$sp();
public abstract byte apply$mcB$sp();
public abstract char apply$mcC$sp();
public abstract double apply$mcD$sp();
public abstract float apply$mcF$sp();
public abstract int apply$mcI$sp();
public abstract long apply$mcJ$sp();
public abstract short apply$mcS$sp();
public abstract void apply$mcV$sp();
}because Scala understands that traits can include default implementations for some methods. from Java, the trait just shows up as an interface, with all methods abstract.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
class A {
def ensure(f: => Unit): Unit = println("ensure")
}
public class B {
public static void main(String[] args) {
A a = new A();
a.ensure(new scala.Function0<scala.runtime.BoxedUnit>() {
@Override
public scala.runtime.BoxedUnit apply() {
return scala.runtime.BoxedUnit.UNIT;
}
});
}
}
def ensure1(f: => Unit): Unit
def ensure2(f: Unit => Unit): Unit
class A {
def ensure1(f: => Unit): Unit = println("ensure1")
def ensure2(f: Unit => Unit): Unit = {
println("before invoke f in ensure2")
f()
}
}
public class B {
public static void main(String[] args) {
A a = new A();
// a.ensure1(new scala.Function0<scala.runtime.BoxedUnit>() {
// @Override
// public scala.runtime.BoxedUnit apply() {
// return scala.runtime.BoxedUnit.UNIT;
// }
// });
a.ensure2(new AbstractFunction1<BoxedUnit, BoxedUnit>() {
@Override
public BoxedUnit apply(BoxedUnit v1) {
System.out.println("invoke ensure2 success");
return BoxedUnit.UNIT;
}
});
}
}