[mercury-users] Bit-fields in Mercury

Michael Day mikeday at bigpond.net.au
Sun Jul 9 14:25:27 AEST 2006


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

Even better, use more informative types than bool:

    f(..., opt(enable_network, disable_parallax, reverse_polarity))

Another option, define a flags type:

:- type opt
    ---> opt1 ; opt2 ; opt3.

and pass a set/list of them:

    f(..., [opt1, opt3])

Typesafe, easy to use and extend. It can't statically detect repeated
options, but if you use an abstract set type these would not be relevant.

Michael

-- 
Print XML with Prince!
http://www.princexml.com
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list