[mercury-users] Constructor question.

Mark Brown mark at csse.unimelb.edu.au
Sat Apr 14 19:26:34 AEST 2012


Hi Michael,

On 14-Apr-2012, Michael Richter <ttmrichter at gmail.com> wrote:
> Given a data type like this:
> 
> :- type foo --->  foo(bar :: int, baz :: string).
> 
> 
> Is there any way to construct a new instance of foo other than this?:
> 
> Foo = foo(10, "eleven").
> 
> 
> Specifically is there a way to use record syntax so that I can name the
> fields being given their initial value?  I've tried a variety of ways but
> just can't see any way to do this that the compiler will accept and, while
> not a problem for a simple record like the above type, it's a bit of a
> nightmare if you're dealing with a type that has, say, a dozen members.
>  I've had to do stuff that looks like this, only with a lot more lines of
> code:
> 
> Foo = foo(  10       % bar
> >             "eleven" % baz
> >          ).
> 
> 
> I find this both very ugly and very error-prone.

You can write

    (_ ^ bar := 10)
       ^ baz := "eleven"

to construct the above term.  If somebody later adds a "quux" field, this
will usually be a mode error because it's not ground - some of the underscore
variable, which starts off free, will remain free in the result.

Cheers,
Mark.

--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the users mailing list