Duck Typing in other languages?

Discussion in 'Ruby' started by Marc Heiler, Aug 29, 2007.

  1. Marc Heiler

    Marc Heiler Guest

    Heya,
    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!
    --
    Posted via http://www.ruby-forum.com/.
    Marc Heiler, Aug 29, 2007
    #1
    1. Advertising

  2. Marc Heiler

    Robert Dober Guest

    On 8/29/07, Marc Heiler <> wrote:
    > Heya,
    > 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.

    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:
    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 ;)
    >
    > 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... ;-)

    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
    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

    >
    > 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!
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >



    --
    I'm an atheist and that's it. I believe there's nothing we can know
    except that we should be kind to each other and do what we can for
    other people.
    -- Katharine Hepburn
    Robert Dober, Aug 29, 2007
    #2
    1. Advertising

  3. Marc Heiler

    Eric Mahurin Guest

    On 8/29/07, Marc Heiler <> 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.
    Eric Mahurin, Aug 29, 2007
    #3
  4. Marc Heiler <> 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 = <fun>

    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
    Benedikt Rosenau, Sep 6, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Duck Typing

    , Sep 16, 2003, in forum: Python
    Replies:
    10
    Views:
    667
    Steve Holden
    Sep 17, 2003
  2. Replies:
    15
    Views:
    553
    Isaac Gouy
    Sep 1, 2006
  3. Replies:
    2
    Views:
    301
    Terry Reedy
    Jan 28, 2008
  4. Paddy
    Replies:
    1
    Views:
    382
    Paddy
    May 9, 2008
  5. Paddy
    Replies:
    0
    Views:
    231
    Paddy
    Jun 25, 2008
Loading...

Share This Page