Duck Typing in Scala: Structural Typing


Scala goes very far with its loads of features improving the JVM world. Sometimes, a bit too far, maybe. But this feature here is just plain awesome! Typesafe duck typing! An example:

def quacker(duck: {def quack(value: String): String}) {
  println (duck.quack("Quack"))
}

The above quacker method accepts any “duck” that can quack.

Read the full article here:
http://java.dzone.com/articles/duck-typing-scala-structural

2 thoughts on “Duck Typing in Scala: Structural Typing

  1. The Go Language also doe something similar to this and is in fact how its “interfaces” work. While I like the idea in theory I find in practice it makes it difficult to read other peoples code. That is my problem with many of the new highly expressive languages: its easier to *write* code but potential much harder to *read* code especially other people’s code.

    Scala I have extra annoyance as it tries to be everything while still being syntactically verbose. I wish Scala was more like F# … focus on being FP, remove the braces (Scala is really not that terse compared to ML) and let other languages be OOP or script like. Embrace Polyglot not C++ 🙂

    With Groovy 2.0 and Java 8, the Vert.x polyglot approach Scala seems less appealing each day. (end of rant 🙂 ).

    • It’s ok, I like your rants 🙂

      I like Scala for its elegant combination of functional and object oriented programming. I really think that it combines the best of both worlds, mostly. But it goes too far in many aspects.

      Then again, this is OK, as I think that Scala is a good sandbox for future Java evolutions. Prior to Scala, no other JVM language was as well-defined and as well-thought-through, in my opinion. Also, no other JVM language had such a capable and passionate leader at its top. Designing a language is just a very hard thing. Fixing an ill-designed language later is almost a no-go (e.g. PHP4 -> PHP5).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s