Hacker News new | ask | show | jobs
by bunderbunder 4473 days ago
Considering how strongly opposed the author is to dynamic typing, I'm actually kind of surprised they'd consider OCaml's type system to be acceptable.

Technically, yes, it's a statically typed system. But its use of structural typing instead of nominative typing effectively means it takes half the compiler assistance you can get out of static type checking and chucks it out the window. Using structural typing means that a type is nothing more than the sum of its parts; nominative typing makes it possible to add further specificity to types by naming them. This is huge. A language that doesn't do this is a language that can't be taught to understand the difference between 12 meters and 12 Newtons.

4 comments

You may be confused by the fact that the type system will notice the equivalence of two types if the implementations of both are public eg

    module M = 
    struct
       type newtons = int
       let inc some_newtons = some_newtons + 1
    end :
    sig
       type newtons = int
       val inc : newtons -> newtons
    end

    M.inc 1 (* this works *)

    module M = 
    struct
       type newtons = int
       let inc some_newtons = some_newtons + 1
       let in_newtons x = x 
    end :
    sig 
       type newtons
       val inc : newtons -> newtons
       val in_newtons : int -> newtons
    end

    M.inc 1 (* type error *)
    M.inc (in_newtons 1) (* this works *)
The use of hidden types in OCaml gives far better control over encapsulation and implementation hiding than any over language I've used.
Abstraction provides the equivalent of nominative typing. It is easy to differentiate between 12 meters and 12 Newtons with OCaml's type system.

In general, it is easy to get nominative behaviour out of a structural system. It is much harder to get structural behaviour out of a nominative system.

Also, only some things in OCaml are structurally typed (modules, objects, polymorphic vairants), others are not (records, variants). For example:

    # type meters = I of int;;
    type meters = I of int

    # let meters i = I i;;
    val meters : int -> meters = <fun>

    # type newtons = I of int;;
    type newtons = I of int

    # let newtons i = I i;;
    val newtons : int -> newtons = <fun>

    # let f b = if b then newtons 12 else meters 12;;
    Characters 36-45:
      let f b = if b then newtons 12 else meters 12;;
                                          ^^^^^^^^^
    Error: This expression has type meters but an expression was expected of type
             newtons
>But its use of structural typing instead of nominative typing effectively means it takes half the compiler assistance you can get out of static type checking and chucks it out the window

It is quite rare for people to use objects in ocaml anyways, so it doesn't matter much.

>A language that doesn't do this is a language that can't be taught to understand the difference between 12 meters and 12 Newtons.

Sure it can. Make a type for meters and a type for newtons. Haskell won't think there is a difference between those if you make them both an int either.