First, let's take a look at the literal translation of Java code into Scala:
class T { def func(a:String) : String = { println(s"There we go: $a") a } } object T { def main(args: Array[String]) = { val supplier = () => new T val f = (t:T) => t.func _ val t = supplier() val v = f(t)("something") println(v) } }
In Scala, functions are first-class citizens, so there is no need to create specific constructs for βthings that generateβ, like the Java Supplier , since it is modeled as a function: f: () => T (the same thing happens for its Consumer counterpart as f: T => () )
We just said that functions are first class citizens, so consider the version above using this paradigm:
object Tfunc {
Scala has no analogue to get a reference to a constructor, but if the goal is to use a functional approach, Scala object offers a simple construction for storing functions and does not require instantiation.
maasg source share