[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ocaml-dns- quick ocaml question



On 18 Jun 2012, at 18:38, Raphael Proust wrote:

> 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)

unfortunately cstruct targets variants only at the moment.

> - use one variant type and add assertion checks or other dynamic failure 
> (bad!)

ack- i thought that didn't "feel" right :)

> - 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

ok- i'll probably go with one of these options then.  (likely the latter.)

was just wondering if there was a "better" way.

> - 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]
>                        *)

interesting!  but possibly a little advanced for me for now :)

thanks!

-- 
Cheers,

R.




This message and any attachment are intended solely for the addressee and may 
contain confidential information. If you have received this message in error, 
please send it back to me, and immediately delete it.   Please do not use, copy 
or disclose the information contained in this message or in any attachment.  
Any views or opinions expressed by the author of this email do not necessarily 
reflect the views of the University of Nottingham.

This message has been checked for viruses but the contents of an attachment
may still contain software viruses which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation.


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.