# Duck Typing in other languages?

**URL:** <https://www.ruby-forum.com/t/duck-typing-in-other-languages/113261>\
**Category:** Ruby\
**Created:** [August 29, 2007, 11:34am UTC](https://www.ruby-forum.com/t/duck-typing-in-other-languages/113261 "2007-08-29T11:34:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [August 29, 2007, 11:34am UTC](https://www.ruby-forum.com/t/duck-typing-in-other-languages/113261/1 "2007-08-29T11:34:36Z")

</div>

Heya,  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502)  
is now a bit older than four years 😉

Bit long, sorry, my “real” question is on the bottom of  
this … anyway 🙂

I think i get that with duck typing you dont care what  
class/type something is. You ask it if it reponds\_to?  
something (and thus has a method)

It sounds a bit as if classes become less important than  
behaviour (i think that’s a good notion anyway) and  
is thus a lot more flexible in general. So far so fine.

When I lately asked on an IRC channel for a prototype based  
language, I unfortunately didn’t really get the answer I hoped  
for (how that language handled “duck typing”) but actually  
it was quickly pointed out that duck typing is used in other  
languages a lot too, maybe with different names )and for  
static languages with a bit more rigid structure).

One definition that kept up was “late bound” (I admit freely,  
duck typing would be approximately 100x easier to understand  
than “late bound”, IF this refers to the same concept)

Smalltalk and Self would be examples for late bound concept.

Java has dynamic class loading and virtual calls (checked at  
class loading time and checked on interfaces) but it seems  
very rarely used (and, it seems you have to do a _new_  
abstract class? How strange is this to create a totally  
new class just to get to this behavioural concept?? But  
then again I may be totally wrong 🙂 )

Basically it seems to boil down (for me) to message sends  
and a class/type “less” approach where the object just  
adapts to a job, instead of satisfying a compiler… 😉

Ok now to my question!

What is the difference between ruby’s approach to classes  
and duck typing compared to prototype based languages which  
seem to have an even looser model (i.e. a less rigid class  
vs object distinction and their usage of prototypes  
“adapting” to a problem from the “bottom-up” ?)

Ah and if I forgot something about duck typing or similar  
in other languages - maybe its interesting - please someone  
feel free to point that out!

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [August 29, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/duck-typing-in-other-languages/113261/2 "2007-08-29T12:23:59Z")

</div>

On 8/29/07, Marc H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It sounds a bit as if classes become less important than  
> behaviour (i think that’s a good notion anyway) and  
> is thus a lot more flexible in general. So far so fine.  
> 100% aligned
> 
> When I lately asked on an IRC channel for a prototype based  
> language, I unfortunately didn’t really get the answer I hoped  
> for (how that language handled “duck typing”) but actually  
> it was quickly pointed out that duck typing is used in other  
> languages a lot too, maybe with different names )and for  
> static languages with a bit more rigid structure).  
> Well maybe I understand incorrectly but I would say by machanisms like  
> adding behavior to objects (I really like the term behavior here)  
> Maybe my ideas might interest you, they are expressed here:  
> [Smalltalking about Ruby: Classless Ruby, a Paradigm Change?](http://ruby-smalltalk.blogspot.com/2007/08/from-method-redefinition-to-anonymous.html)  
> you will see how I got rid of classes completely, in Ruby.
> 
> One definition that kept up was “late bound” (I admit freely,  
> duck typing would be approximately 100x easier to understand  
> than “late bound”, IF this refers to the same concept)  
> I think not really, I would say late binding is extremely useful for  
> the agility of a language but behavior could be fixed at compile time  
> without any class or is\_a? concept and we still would have duck\_typing  
> objects, but I feel that without late binding it is less fun, maybe  
> not even worth it, hmm that is food for thaught.  
> It would be nice if we could just assume Late Binding anyway 😉  
> Basically it seems to boil down (for me) to message sends  
> and a class/type “less” approach where the object just  
> adapts to a job, instead of satisfying a compiler… 😉  
> Yup and in the end it might not even matter if this behavior was  
> created by instantiation from a class or not, maybe you should have a  
> look at this too:  
> [http://www.iam.unibe.ch/~scg/Archive/PhD/schaerli-phd.pdf](http://www.iam.unibe.ch/~scg/Archive/PhD/schaerli-phd.pdf)  
> It is about Traits, anyway the way how to induce behavior into objects  
> is a very interesting field, strictly speaking it is not 100% bound to  
> duck typing as long as one is not hindered by Compiletime (or even  
> Runtime) checks looking at labels sticked onto objects, right?

Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [August 29, 2007, 3:35pm UTC](https://www.ruby-forum.com/t/duck-typing-in-other-languages/113261/3 "2007-08-29T15:35:41Z")

</div>

On 8/29/07, Marc H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ah and if I forgot something about duck typing or similar  
> in other languages - maybe its interesting - please someone  
> feel free to point that out!

Another place I’ve noticed similar functionality is with  
templates/generics in C++/Java. You write code that doesn’t care  
about the (template/generic) classes/types. You write code for  
objects of template/generic classes that respond to a set of methods  
that you use. Compared to duck-typing in ruby, the main difference is  
that concrete classes that you want to use for these template/generic  
classes must be determined at compile-time. And of course  
template/generic code is extremely ugly because it has to look like  
static typing even those the types are variable. Also, with  
template/generic code, the code is replicated for each class/type  
combination, where with duck-typing this is not needed since finding  
the methods occurs at run-time.

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [September 6, 2007, 5:26pm UTC](https://www.ruby-forum.com/t/duck-typing-in-other-languages/113261/4 "2007-09-06T17:26:21Z")

</div>

Marc H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I think i get that with duck typing you dont care what  
> class/type something is. You ask it if it reponds\_to?  
> something (and thus has a method)

Duck typing is not formally defined. If it means similar  
interfaces, it can be inferred:

$ ocaml  
let duck ob arg = print\_int (ob#meth arg);;  
val duck : \< meth : 'a → int; … \> → 'a → unit =

The type inference finds that ob is an object with the method  
meth which returns an integer. There is even polymorphism in  
the argument allowed. I wonder if functors qualify as duck  
typing on a more general level.  
Benedikt
