Hacker News new | past | comments | ask | show | jobs | submit login

Static typing also makes overloading your f.unctions on return type instead of just arguments types possible. Not a lot of languages do this, though. I only know of one, but it's invaluable there.



Perl's a dynamic language, but return type can vary based on context. I once saw this really bite someone where the presence of parentheses on the left-hand side of the expression changed the behavior of the function being called on the right-hand side. Not fun to debug that one.


Exactly. Duck typing sucks. I don't know why it was invented...


It only "sucks"(causes problems rarely, and can be quite useful regardless) in dynamic languages. Languages like Go, Rust, and Haskell provide the same flexibility with static guarantees. All you must do is define type specific implementations to satisfy the interface(or typeclass). For example in Haskell, I can so something like:

    class Stream s where
      read :: s -> (a, s)
      write :: a -> s -> s
and extend it to any type:

    instance Stream [Int] where
      read s = ...
      write a s = ...

    instance Stream File where
      ...
and so on, and I can now pass any type that is a member of Stream, to a function that expects one.


The haskell code is — as far as I can see anyway — nominative typing. It can be post-implemented, but you still need your type to be explicitly made into a Stream instance.

Contrast OCaml, an object type is represented as a set of (method, types) tuples and type-checking is a subset check (if type A has all the methods of type B, then it's a subtype of B regardless of anything else from visibility to semantics):

    # let x =
          object
              method foo = 42
          end;;
    val x : < foo : int > = <obj>
    # let y =
          object
              method foo = 63
              method bar = 12
          end;;
    val y : < bar : int; foo : int > = <obj>
    # x = y;;
    Error: This expression has type < bar : int; foo : int >
           but an expression was expected of type < foo : int >
           The second object type has no method bar
    # type simple = < foo : int >;;
    type simple = < foo : int >
    # (y :> simple) = x;;
    - : bool = false
    #


You're comparing ad-hoc polymorphism with subtype polymorphism. Doing so will lead you to the expression problem:

http://en.wikipedia.org/wiki/Expression_problem

Anyway, there's been quite a few proposals to add extensible records to Haskell, which would allow row polymorphism, similar to what you just showed in OCaml:

http://hackage.haskell.org/trac/ghc/wiki/ExtensibleRecords

Too bad that it has gone nowhere in a long time.


> You're comparing ad-hoc polymorphism with subtype polymorphism.

No, I'm comparing structural typing, which is what Go implements, to nominative typing.

But that may very well be due to me having stayed in context of an other sub-thread where this was the subject, and using that as a filter for the current one.


I really wish more languages had the option of using a Haskell style type system.


Or even just plain Hindley-Milner.


The above is not an example of duck typing.

Even if it was, one example of a difficult bug in one system does not invalidate a whole concept.

And duck typing is almost as old as programming itself...




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: