Scala Goodness: Compound Types

Scala is ideally suited for programming in roles because it does features traits. Think of Traits like interfaces in Java with implementation code. Say you have a dog:

then this dog has two roles, Callable and Feedable. You can extract those into traits:

and combine them with your Dog:

This is the first Scala goodness. But you can also extend your Dog at calltime:

When you've developed Java for some time and adhere to the usage of interfaces for roles, you surely have tripped over the problem that one of your methods expects a parameter object to implement two interfaces (sometimes this is a sign that the method does too much) and you resorted to adding an object twice to the method signature:

The call with twice the dog really does look ugly. And you get into shallow water semantically. Does the method call the two methods on two objects? Shouldn't it call them on one method after each other?

Update: As Daniel pointed out, in Java one can write:

In Scala the method can be expressed nicer with a compound type as the parameter type:

Scala glory!

Comments are closed.