the blog for developers

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:

class Dog {
   def call() { println("Comes to you") }
   def feed() { println("Feeds") }
}

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

trait Callable {
  def call() { println("Comes to you") }
}

trait Feedable {
   def feed() { println("Feeds") }
}

and combine them with your Dog:

class Dog extends Callable with Feedable

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

scala> class Dog
defined class Dog

scala> val d = new Dog
d: Dog = Dog@1f808e6

scala> d.call
:7: error: value call is not a member of Dog
       d.call
         ^
scala> val d = new Dog with Callable with Feedable
d: Dog with Callable with Feedable = $anon$1@1bca486

scala> d
res5: Dog with Callable with Feedable = $anon$1@1bca486

scala> d.call
Comes to you

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:

public void callAndFeed(Callable c, Feedable f) {
   c.call();
   f.feed();
}

callAndFeed(dog,dog);

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:

public  void callAndFeed(T cf) {
  cf.call();
  cf.feed();
}

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

scala> def callAndFeed(d:Callable with Feedable) {
  d.call();
  d.feed();
}
callAndFeed: (Callable with Feedable)Unit

scala> callAndFeed(d)
Comes to you
Feeds

Scala glory!

You can leave a Reply here. Of course, you should follow me on twitter here.

You can share this post!
Do you want to tell others about this article? Use the social bookmark icons to submit this artice to the service of your choice. Thanks.

About the author: Stephan Schmidt has more than 15 years of internet technology experience and 10 years experience in agile. He was head of development, consultant and CTO and is a speaker, author and blog writer. He specializes in organizing and optimizing software development helping companies by increasing productivity with lean software development and agile methodologies. Want to know more? All views are only his own.

20 Tweets

Leave a reply.

Comments

Strictly speaking, Java supports “compound types” as well, at least in the example you gave of the method parameter requiring two interfaces:

public <T extends Callable & Feedable> void callAndFeed(T cf) {
cf.call();
cf.feed();
}

@Daniel: Excellent, thx, helps me with current code!

@Stephan – great post on indeed very useful Scala feature.
@Daniel – you are right, but in Java the support for compound types is very limited. Compound types can only be used in generic methods, they can only compose of interface types (and java.lang.Object for erasure tricks), and they don’t support type parameters. While Java’s conservative approach is understandable, in Scala, you can really go wild and define types like “String with Integer” or “T with S” and even do something useful with it, like the HTMap (http://github.com/dlwh/scalanlp-core/blob/master/src/main/scala/scalanlp/collection/immutable/HTMap.scala)

I only wished Scala didn’t only support roles/traits but also first-class collaborations. The action is in the interaction of the roles, not in the class interface combining the roles.

[...] Scala Goodness: Compound Types Ja, Scala ist auf jeden Fall einen genauen Blick wert … [...]

Viktor Klang

Dirk: You can, with self-type declarations

@Yardena, “String with Integer” is a type with no values, pretty much useless, which makes it a poor example :)

@Dirk, the types define allowable collaborations. So what do you feel is missing?

@stephan, I wanted to see a “Scala Goodness” blog for some weeks now – since I was seeing “Groovy Goodness” floating around, and continuous marketing can result in more mindshare, so, thanks :)

@Dimitris you don’t necessarily need a value of a type to make it useful. Nothing is a very useful type :-) Seriously though – sometimes you just need to express a certain constraint for the compiler to check. Take a look at HTMap – you put 5 and “hello” into it, get an HTMap[Int with String] and then obtain 5 from it with get[Int] and “hello” with get[String].

@Dimitirs: “… continuous marketing can result in more mindshare, so, thanks :)”

Thought the same about continuous marketing.

Pooria

“Think of Traits like interfaces in Java with implementation code.”

That makes me think of abstract classes in C++ with implementation code!

Leave a Reply

What people wrote somewhere else:

Blogged a short post “Scala Goodness: Compound Types” http://bit.ly/7exDiW #scala #typesystem

This comment was originally posted on Twitter

RT @codemonkeyism: Blogged a short post “Scala Goodness: Compound Types” http://bit.ly/7exDiW #scala #typesystem

This comment was originally posted on Twitter

RT @codemonkeyism: Blogged a short post “Scala Goodness: Compound Types” http://bit.ly/7exDiW #scala #typesystem

This comment was originally posted on Twitter

:) nice RT: @codemonkeyism: Blogged a short post “Scala Goodness: Compound Types” http://bit.ly/7exDiW #scala #typesystem

This comment was originally posted on Twitter

Blogged a short post “Scala Goodness: Compound Types” http://bit.ly/7exDiW #scala #typesystem (via @codemonkeyism)

This comment was originally posted on Twitter

Runtime compund inheritance, nice. http://bit.ly/8W9cVH

This comment was originally posted on Twitter

Scala Goodness: Compound Types http://codemonkeyism.com/compound-types-scala-goodness/ 其实也没什么特别的,当作茶歇的小点心吧

This comment was originally posted on Twitter

RT @codemonkeyism: Blogged a short post “Scala Goodness: Compound Types” http://bit.ly/7exDiW #scala #typesystem

This comment was originally posted on Twitter

programming compound types in #scala /by @codemonkeyism http://icio.us/kash3t

This comment was originally posted on Twitter

Code Monkeyism: Scala Goodness: Compound Types http://ff.im/-dyUQ4

This comment was originally posted on Twitter

RT @jboner: RT @codemonkeyism: Blogged a short post “Scala Goodness: Compound Types” http://bit.ly/7exDiW #scala #typesystem

This comment was originally posted on Twitter

@CuriousSkeptic Speaking of Role based oop, have a look at: http://codemonkeyism.com/compound-types-scala-goodness/

This comment was originally posted on Twitter

Compound Types in Scala http://ff.im/-dAsba

This comment was originally posted on Twitter

Code Monkeyism: Scala Goodness: Compound Types http://ff.im/-dBNzK

This comment was originally posted on Twitter

RT @codemonkeyism Code Monkeyism: Scala Goodness: Compound Types http://bit.ly/7exDiW

This comment was originally posted on Twitter

Code Monkeyism: Scala Goodness: Compound Types http://ff.im/-dDmzq

This comment was originally posted on Twitter

Code Monkeyism: Scala Goodness: Compound Types http://ff.im/-dE7Xx

This comment was originally posted on Twitter

Code Monkeyism: Scala Goodness: Compound Types http://ff.im/-dL3fs

This comment was originally posted on Twitter

Code Monkeyism: Scala Goodness: Compound Types http://ff.im/-dMqo9

This comment was originally posted on Twitter

Code Monkeyism: Scala Goodness: Compound Types: Scala is ideally suited for programming in roles because it doe.. http://bit.ly/4tMk3C

This comment was originally posted on Twitter

Additional comments powered by BackType

Guide to CodeMonkeyism

Over the last 4 years I wrote many articles on this blog. To make it easier for you to find the relevant ones, I've organized them into topics.

Top 10

6 reasons why my VC funded startup did fail

Go Ahead: Next Generation Java Programming Style

Java Interview questions: Write a String Reverser

The dark side of NoSQL

7 Bad Signs not to Work for a Software Company or Startup

Is Java dead?

Scala vs. Clojure

Never, never, never use String in Java

No future for functional programming in 2008 – Scala, F# and Nu

Clojure vs Scala, Part 2

Java Developer

Is Java Dead?

Go Ahead: Next Generation Java Programming Style

Be careful with magical code

All variables in Java must be final

Never, never, never use String in Java

Bending Java: More readable code with methods that do nothing?

NoSQL Guy

NoSQL: The Dawn of Polyglot Persistence

The dark side of NoSQL

Essential storage tradeoff: Simple Reads vs. Simple Writes

Sharding destroys the goals of your relational database

The unholy legacy of databases

Startup/CTO

Development Dream Teams

6 reasons why my VC funded startup did fail

American vs. European style of Software Development

12 Things to Reduce Your Lead Time and Time to Market

The high cost of overhead when working in parallel

Essential storage tradeoff: Simple Reads vs. Simple Writes

Job Seeker

Another Good (Java) Interview Question

7 Bad Signs not to Work for a Software Company or Startup

Java Interview questions: Write a String Reverser (and use Recursion!)

Java Interview questions: Multiple Inheritance

As a Manager: What I value in developers

Top 10 Tips (+1) to Get a Pay Raise

Agilist

What Developers Need to Know About Agile

5 Practices Better to Change in Your Scrum Implementation

Scrum is not about engineering practices

ScrumMaster and ZenMaster: The joke of certification

What is Trans-Scrum?