Passing values taking implicit parameters / smart constructors

Malcolm Wallace Malcolm.Wallace@cs.york.ac.uk
Thu, 31 Jan 2002 10:41:32 +0000


> > I think that what you want here are 'smart constructors', i.e. functions
> > that create a new value but also perform other initializations.
> ..
> > If you define Tree and the smart constructors in a separate module and then
> > only export the type Tree (*not* the real constructors!),
> 
> But then I cannot use record sytax (updates, pattern matching)
> outside the defining module.

Actually, you *can* use named field syntax for updates (provided the
field names are exported from the defining module).

e.g.
    f tree = .... tree { top=..., left=... }

although it is true that you then lose the `smart constructor'
property that ensures (for instance) that the size field is calculated
automatically.

Pattern matching does indeed remain impossible (because the constructor
is not exported).  However, you could use the field names as selectors,
which is a little more cumbersome, but not too bad.

e.g.
    f tree = ....
          where t = top tree
                l = left tree
                s = size tree
          
instead of
    f (Tree {top=t, left=l, size=s}) = ....

Regards,
    Malcolm