Scala Goodness: Structural Typing
Structural typing in Scala is the way to describe types by their structure, not by their name as with other typing. Structural typing reduces coupling and the need for inheritance. In Java you would mainly use interfaces instead of structural typing.
I go with the same examples as in “Scala Goodness: Compound types” so it’s easier to compare both solutions.
class Dog {
def call() = println("Comes to you")
}
class Cat {
def call() = println("Doesn't bother")
}
def call(c:{ def call():Unit }) = c.call();
c:{ def call():Unit } describes a type which has a call method that returns nothing – like void in Java. The method does take all objects of types that satisfies this constraint. We can now use call on both classes:
scala> call(new Dog) Comes to you scala> call(new Cat) Doesn't bother
You can also give your structural type a name with type aliasing:
type Callable = { def call():Unit }
def call(c:Callable) = c.call();
With the same classes from above we get:
scala> call(new Cat) Doesn't bother scala> call(new Dog) Comes to you
Both ways work with more than one method:
def callAndFeed(a:{ def call():Unit; def feed():Unit }) {
a.call();
a.feed();
}
class Dog {
def call() = println("Comes to you")
def feed() = println("Eats")
}
Calling with a new dog results in:
scala> callAndFeed(new Dog) Comes to you Eats
Structural typing is very useful if you are not able to modify classes to make them implement a trait or interface or if you want to reduce coupling and increase reuse. How does this relate to interfaces? The benefit of interfaces instead of structural typing is how they describe the roles of a class.
class Dog extends Feedable with Callable
instead of
class Dog {
def call() = println("Comes to you")
def feed() = println("Eats")
}
Scala glory!
See also:
- Scala Goodness: Extractors
- Scala Goodness: RichString
- Scala Goodness: Compound Types
- Scala Goodness: Tuples
You can leave a Reply here. Of course, you should follow me on twitter here.


I particularly like the use of structural typing in the “using” control structure from Beginning Scala chapter 4:
def using[A < : {def close(): Unit}, B] (param: A)(f: A => B): B = try { f(param) } finally { param.close() }Now you can safely use any object with a close() method and be assured it will be closed when you’re done. A lot of Java classes implement the java.io.Closeable interface, but many do not (JDBC’s Connection, Statement and ResultSet are a few).
val conn: Connection = …
using (conn) { conn => //do stuff with conn and it’ll get closed }
using (new BufferedReader(new FileReader(“file”)) { reader => //do stuff with reader and it’ll get closed }
using (new PrintWriter(new FileWriter(“file”)) { writer => //do stuff with writer and it’ll get closed }
Structural types used to not be thread-safe: https://lampsvn.epfl.ch/trac/scala/ticket/1006, but apparently this has been fixed. Not sure if it’s fixed in 2.7 or just 2.8?
(Stephan: Fixed displayed code)