Scala

Scala Apply Method

The apply methods in scala has a nice syntactic sugar. It allows us to define semantics like java array access for an arbitrary class. For example, we create a class of RiceCooker and its method cook to cook rice. Whenever we need to cook rice, we could call this method. class RiceCooker { def cook(cup_of_rice: Rice) = { cup_of_rice.isDone = true cup_of_rice } } val my_rice_cooker: RiceCooker = new RiceCooker() my_rice_cooker.

Continue reading

Constructor - Scala vs. Java

1. Constructor With Parameters Java Code public class Foo() { public Bar bar; public Foo(Bar bar) { this.bar = bar; } } Scala Code class Foo(val bar:Bar) 2. Constructor With Private Attribute Java Code public class Foo() { private final Bar bar; public Foo(Bar bar) { this.bar = bar; } } Scala Code class Foo(private val bar:Bar) 3. Call Super Constructor Java Code public class Foo() extends SuperFoo { public Foo(Bar bar) { super(bar); } } Scala Code

Continue reading