[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: ocaml-dns- quick ocaml question
On Mon, Jun 18, 2012 at 5:26 PM, Richard Mortier <Richard.Mortier@xxxxxxxxxxxxxxxx> wrote: > i have two types, rr_type and q_type. rr_type is a subset of q_type, extended > it by about 5 elements. both are currently implemented in Dns.Packet as > cenums which are translated to variant types. > > i've just realised that (of course?) this means that the last definition > (rr_type) overrides the first (q_type) as they have overlapping elements. > > is there a nice/idiomatic way around this without either going to polymorphic > variant types (that's what the `T types are called, right?) or just defining > the cenum q_type with rr_type aliased to it (for slightly better > readability), and using that everywhere? > > eg., should i do something like create submodules Qtype and RRtype of Packet, > and define cenum t in each? I see several possibilities: - use polymorphic variants (yes that's what `Ts are called) - use one variant type and add assertion checks or other dynamic failure (bad!) - use several submodules - prefix the variants (very common) (feels similar to submodules but prevents "open"-induced ambiguities) type q = | Q_Foo | Q_Bar of int type rr = | RR_Foo | RR_Bar of int | RR_Baz of float - use GADTs (requires OCaml 4.00 (still in beta)) (I am unsure of the proper syntax, check the manual if need be) type q type rr type 'a t = | Foo : 'a t | Bar : int -> 'a t | Baz : float -> q t (* This branch forces the ['a] to be instantiated with [q], thus preventing anyuse with functions requiring [rr t] *) Cheers, -- _______ Raphael
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |