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

Traits

The concept of an object-oriented interface is encapsulated in a trait in Scala. Similarly to an interface, a trait can have abstract members. However, unlike Java interfaces, traits may also have concrete members. These are injected into the implementing classes:

scala> :paste// Entering paste mode (ctrl-D to finish)trait Foo {  def saySomething = println("I am inherited from Foo")}// Exiting paste mode, now interpreting.defined trait Foo

Just like in Java, Scala classes can implement more than one trait. However, since traits in Scala can have concrete members, a new inheritance model that allows for that is required.

In Scala, a so-called linearization model is implemented. This means that whenever a class is inherited from ...

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