Duck Typing advocated where?

I used to think, (way, way, back), that Duck Typing was for dynamically typed languages. Involvement in developing the wikipedia page showed my naïveté, but I am left thinking "is duck typing advocated in languages other than the dynamic ones"?

I know that Boo has the specific duck as a type so thats one, any others?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Scala recently added structural types

See the Scala changelog:

It is now possible to declare structural types using type refinements. For example:

class File(name: String) {
  def getName(): String = name
  def open() { /*..*/ }
  def close() { println("close file") }
def test(f: { def getName(): String }) { println(f.getName) }

test(new File("test.txt"))

Structural vs Duck

Ah, from this definition, it seems that structural types are similar, but Duck typing just needs the sub-structure that is used to be compatible.


Structural subtyping

Basically, structural subtyping (or other forms of structural polymorphism, e.g. row polymorphism) gives you the same flexibility.

Structural subtyping is used in ML modules, for example. Row polymorphism appears in OCaml's object system.

Duck Typing and Structural Subtyping

I think "Duck Typing" is just structural subtyping, with the added implication that the language doesn't attempt to statically verify anything. It's true but misleading to say that structural subtyping checks the argument for all parts of the expected type rather than just the part that is used - nice languages with structural subtyping and anonymous object types (like O'Caml) will infer an expected type that contains mostly just the parts you actually use anyway.

As usual, if you have been "too tricky" you may have to do a little more work to make your program obviously sound (to the type system), the payoff being that the type system warns you if you do something not "obviously" sound.

Boo's 'duck' is still dynamic, I think.

While Boo is primarily statically typed, operations on "duck" aren't statically checked, so it doesn't count as static structural subtyping.

static duck typing

Static duck typing is already a central functionality in all languages that provide for genericity or type inference.
Examples are C++ and the ML family of programming languages.