Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] Width subtyping
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Dario Teixeira <darioteixeira@y...>
Subject: Re: [Caml-list] Width subtyping

Hi,

> There must be something that escapes me.... This seems to be an
> example where ocaml objects really shine because of the structural
> typing (i.e. an object is defined by the its structure):

You are right.  I was probably too fixated on the OOP way of doing this,
translating record fields into object fields and the functions acting on
records into object methods.  Your solution, where record fields become object
methods and external functions act on objects that match a certain structure,
does solve the inconveniences I mentioned before.  But objects are still
a somewhat heavier solution, right?

In the meantime I also investigated Polymap, and it is close to being the
ideal solution.  It has one huge drawback, though: type inconsistencies
cannot be detected at compile-time, producing instead runtime exceptions.
The example below illustrates this; note that function printx should accept
both m1 and m2, but not m3.  However, the mistake in 'foobar' is not reported
until runtime:  (In fairness, there's probably no easy technical fix for this.)

let printx m = Printf.printf "X is %d and Y is %s\n" m.`x m.`y

let m1 = `{x=1; y="foo"}
let m2 = `{x=2; y="bar"; z=true}
let m3 = `{y="ola"; z=false}

let foobar =
        printx m1;
        printx m2;
        printx m3

Cheers,
Dario