English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Parallelized parsing
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-04-21 (01:44)
From: Eliot Handelman <eliot@c...>
Subject: Polymorphism problem
Hi list,

Consider this:

type 'a x = { x_v : 'a }

and 'a y = { y_x : int kind;
         y_arr : 'a array
and 'a kind =
    X of 'a x
  | Y of 'a y

I'd like to write a function _getter_ that's polymorphic over kind. This
doesn't work, getting int kind -> int:

let rec getter = function
    X x -> x.x_v
  | Y y -> y.y_arr.(getter y.y_x)

which seems surprising to me since getter y.y_x is an intermediate
value that's never returned. Is this just a limitation of the type system or
does this result make sense?

Here's my workaround:

let rec int_getter = function
    X x -> x.x_v
  | Y y -> (int_getter y.y_x)

let rec getter = function
    X x -> x.x_v
  | Y y -> y.y_arr.(int_getter y.y_x)

where now the type of getter is 'a kind -> 'a as needed. I have no
choice but to use this at present -- is there a better method?

thanks for wisdom,

-- eliot