Skip to content

Comment on Why OCaml, Why Now?parent

Comments

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
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.