Ninja Ninja - 2 months ago 8
Scala Question

Explain the usage of Abstractfunction1 in scala

Can you guys help me to understand the usage of scala.runtime.AbstractFunction1 in scala. Why we need to override the apply method in it .

Answer

All classes in scala.runtime are just implementation details used by code generated by the compiler. They shouldn't normally appear in user code.

There is one exception: if you need to call a method taking scala.Function* (where * is the number of arguments) from Java, you should use a (usually anonymous) class extending scala.runtime.AbstractFunction*, because the Java compiler doesn't know about Scala traits. But that isn't a "usage of scala.runtime.AbstractFunction1 in scala", so not relevant to this question.

You can also extend scala.runtime.AbstractFunction* instead of scala.Function* in Scala, but this has the same result (with slightly smaller generated code).

Comments