Browse thread
mutually dependent class and type
-
Sébastien Hinderer
- Yann_Régis-Gianas
- Martin Jambon
- Tim Rentsch
[
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: | Tim Rentsch <txr@a...> |
| Subject: | Re: [Caml-list] mutually dependent class and type |
> Date: Fri, 26 Sep 2008 00:24:39 +0200
> From: =?iso-8859-1?Q?S=E9bastien?= Hinderer <Sebastien.Hinderer@ens-lyon.org>
>
> Is there a (clean) way to define simultaneously a class and a type that
> are mutually recursive ?
> Something like this :
> class element (c : content) =
> object
> ...
> end and type content =
> | Data of string
> | Elements of element list;;
>
> This is of course not a valid OCaml definition, but is there a way to
> express it ?
Frequently a good way around such problems is to abstract the type
definition with respect to the mutually dependent defined item.
Then, supply the appropriate parameter when defining the class:
type 'a generic_content =
| Data of string
| Elements of 'a list
class element (c : element generic_content) =
object
method c_value = c
end
Voila! The class is defined in terms of a type that depends on
the class.
For classes, this technique produces a nicer result if applied to
a class type rather than a class directly. Then we can define
the desired type 'content' appropriately and use it in the
class definition:
type 'a generic_content =
| Data of string
| Elements of 'a list
class type element_t =
object
method c_value : element_t generic_content
end
type content = element_t generic_content
class element (c : content) =
object
method c_value = c
end
This technique of parameterizing a type definition with
respect to a dependent type is a good one to know; it
also is sometimes useful with modules/functors.