Version française
Home     About     Download     Resources     Contact us    
Browse thread
Subtyping
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Goswin von Brederlow <goswin-v-b@w...>
Subject: Re: [Caml-list] Subtyping
Goswin von Brederlow <goswin-v-b@web.de> writes:

> So what other ways are there of doing this? Records. Idealy I would
> like to do this:
>
> type base = { x : int }
> let make_base x = { x = x }
> let print_x r = print_int r.x
> type foo = { base with y : int }
> let make_foo x y = { x = x; y = y }
> let _ =
>   print_x (make_base 0); print_newline ();
>   print_x (make_foo 0 1); print_newline ();;
>
> Ocaml has no way of making this work like that, right?

Small extra question concerning this. Can I get ocaml to recognise a
type like this?

type base = 'a. {
  x : 'a;
  fn : 'a -> unit;
}

List.iter
  (fun r -> r.fn r)
  [{x = 1; fn = (fun r -> print_int r.x); };
   {x = 1.2; fn = (fun r -> print_float r.x); }]

The difference to a "'a base" type would be that the 'a is only
infered and fixed inside the record but remains abstract outside of
it.

For that reason this would not be allowed:

let cross_call x y = x.fn y

The 'a would have to escape the record which wouldn't be allowed.


So is there any syntax or trick to get the ocaml typesystem to
construct such a type?

MfG
        Goswin