Skip to Content
Mastering Functional Programming
book

Mastering Functional Programming

by Anatolii Kmetiuk
August 2018
Intermediate to advanced
380 pages
10h 2m
English
Packt Publishing
Content preview from Mastering Functional Programming

Classes

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Functional Programming

Learning Functional Programming

Jack Widman

Publisher Resources

ISBN: 9781788620796Supplemental Content