August 2018
Intermediate to advanced
380 pages
10h 2m
English
Classes in Scala have similar semantics to their Java counterparts. They are defined as follows:
scala> :paste// Entering paste mode (ctrl-D to finish)class Dummy(constructorArgument: String) { var variable: Int = 0 val value: String = constructorArgument * 2 def method(x: Int): String = s"You gave me $x"}// Exiting paste mode, now interpreting.defined class Dummyscala> new Dummy("Foo")res15: Dummy = Dummy@1a2f7e20scala> res15.variableres16: Int = 0scala> res15.valueres17: String = FooFooscala> res15.method(2)res18: String = You gave me 2
Also, it is possible to define so-called case classes in Scala. These classes are used to represent product types, that is, several types bound together in one datatype. For example, you can define ...