Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duck typing and generic programming

I searched through SO for a while and I could not find a definite and general answer, only some contradictory and particular opinions. [1]

So I would like to know what is the relationship between duck typing and generic programming? ( DT < GP , DT == GP, DT > GP ) . By generic programming I refer to ,in particular, C++ templates or Java generics, but a general answer, related to the concepts, if it's possible, would be welcomed.

I know that generic programming will be handled at compile time, while duck typing will be handled at runtime, but appart of this I do not know how to position them.

Lastly, I do not want to start a debate, so I would prefer answers like reasons for, reasons against.

[1] What's the relationship between C++ template and duck typing?

like image 783
coredump Avatar asked Sep 17 '12 10:09

coredump


3 Answers

I have encountered two different definitions of "Duck Typing". No doubt there are people who will tell you that one of them is "correct" and the other is "incorrect". I merely attempt to document that they're both used rather than tell you that they're both "correct", but personally I see nothing wrong with the broader meaning.

1) runtime-only typing. Type is a property of objects, not variables, and hence necessarily when you come to call a method on an object, or otherwise use properties that it has by virtue of its type, the presence or absence of that method is discovered at runtime[*]. So if it "looks like a duck and quacks like a duck" (i.e. if it turns out to have a quack() function) then it "is" a duck (anyway, you can treat it like one). By this definition of course C++ templates fall at the first hurdle, they use static typing.

2) A name used more generally for the principle that if it looks like a duck and quacks like a duck then it is a duck, to mean any setup in which interfaces are implicitly defined by the operations performed by the consumer of the interface, rather than interfaces being explicitly advertised by the producer (whatever implements the interface). By this definition, C++ templates do use a kind of duck-typing, but whether something "looks like a duck" is determined at compile time, not at runtime, based on its static type rather than its dynamic type. "Check at compile time, can this variable's static type quack?", not "check at runtime, can this object quack?".

The dispute seems to me in effect to be over whether Python "owns" the term, so that only Python-like type systems can be called duck typing, or whether others are free to appropriate the term to mean a similar concept in a different context. Whatever you think it should mean, it seems irresponsible to use a "jokey" term and demand that everyone understands the same formal definition from it. SO is not a suitable forum to tell you what a term "should" mean, unless there's an authoritative source that you're asking about, like a dictionary or any academic papers that define it formally. I think it can tell you what it's actually used to mean.

"Generic programming" can make use of duck typing or not, depending on the exact details. C++ generic containers do use "type 2" duck-typing, because for a general type in C++ it's not guaranteed that you can copy it, compare it, get a hash value, etc. Java generic containers don't, Object has enough methods already to make it hashable etc.

Conversely, I suspect that anything you do that uses duck typing, can reasonably be referred to as "generic programming". So I suppose, in the terms you asked for, GP > DT in that duck typing (either definition) is a strict subset of the vast range of stuff that can be called "generic".

[*] well, in some cases your dynamic language could have some static analysis that proves the case one way or the other, but the language demands the ability to defer this check until runtime for cases where the static analysis cannot conclusively say.

like image 50
Steve Jessop Avatar answered Sep 21 '22 00:09

Steve Jessop


This is really a question of vocabulary. In it's most general sense, generic programming is unrelated to the issue of compile-time vs. run-time: it's a solution to a general problem. A good example of where generic programming is run-time is Python, but it's also possible to implement run-time generic programming in C++ (at a significant cost in execution time).

Duck typing is an orthogonal concept, and is usually used to imply runtime typing. Again, the most frequently cited modern example is Python, but many, many languages, starting with Lisp, have used it in the past. As a general rule, both C++ and Java have made an explicit choice not to support duck typing. It's a trade-off: safety vs. flexibility (or compile-time errors vs. run-time errors).

like image 26
James Kanze Avatar answered Sep 21 '22 00:09

James Kanze


Java doesn't support duck typing in the language. It does support reflection which can achieve the same thing. It doesn't have any relationship with Java's Generics as far as I can see, in fact getting them to work together is a real pain.

like image 24
Peter Lawrey Avatar answered Sep 23 '22 00:09

Peter Lawrey