[mercury-users] Bit-fields in Mercury

Jonathan Morgan jonmmorgan at gmail.com
Mon Jul 10 22:19:48 AEST 2006


On 7/9/06, Michael Day <mikeday at bigpond.net.au> wrote:
>
> > How (if at all) would this sort of thing be represented in Mercury?
>
> You could also try this:
>
> :- type opt
>     --->    opt(
>                 opt1 :: bool,
>                 opt2 :: bool,
>                 opt3 :: bool
>             ).
>
> And use it like this:
>
>     f(..., opt(yes, no, no))
>
> Has the advantage that adding an extra option to the type will break all
> code that uses it, forcing people to check whether they really mean yes or
> no. (This can be considered a disadvantage as well :)

Generally in C these constructs are used to provide defaults, making
this style of layout cumbersome.  I certainly don't approve of the set
of bools, as it doesn't provide any readability at all (also these
types can be used to provide more than two options by using more than
one bit).

> Even better, use more informative types than bool:
>
>     f(..., opt(enable_network, disable_parallax, reverse_polarity))

This is more usable, but you are still left with the problem that it
does not allow the use of (often sensible) defaults.

> Another option, define a flags type:
>
> :- type opt
>     ---> opt1 ; opt2 ; opt3.
>
> and pass a set/list of them:
>
>     f(..., [opt1, opt3])

I quite like this one.  I would prefer in these situations to have
types that are specific to the function that is being used, rather
than generic bitfields.

In most cases this shouldn't be a problem, as you would be better
having a couple of enumerations as separate arguments than combining
them all into one argument.  It works well for C, but isn't
necessarily the best thing to port to Mercury.

I think my original version was the best literal representation of the
C type, but this caused it a lot of disadvantages in a more strongly
typed language.

Jon
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at csse.unimelb.edu.au
administrative address: owner-mercury-users at csse.unimelb.edu.au
unsubscribe: Address: mercury-users-request at csse.unimelb.edu.au Message: unsubscribe
subscribe:   Address: mercury-users-request at csse.unimelb.edu.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list