[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| 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