[mercury-users] Newbie questions about typeclasses

Critterrathman at aol.com Critterrathman at aol.com
Wed Apr 25 01:26:50 AEST 2001


Thanks to both Ferguson and Mark for answering my questions!!!  Quite helpful 
in getting me past the rut I was stuck in.

>>When compiling multi-module programs, it can be difficult
>>to manually figure out which interface files need to be generated, so we
>>recommend you use mmake to do it automatically.  E.g.:
>>    mmake main_module.depend
>>    mmake main_module

Don't you just hate users that ignore the documentation.  I had been using 
mmake a while back but when I ran into trouble I started using MMC (not that 
it helped).  For some reason, I never went back to the recommended method.  
Works without a hitch using mmake.

>>It sounds like you want abstract typeclass instances.  In the interface
>>section, mention the type and instance but don't give any definition,
>>like so:
>>    :- type circleRecord.
>>    :- instance shapeClass(circleRecord).
>>    :- instance circleClass(circleRecord).

Ok.  Makes sense.  Works as I expect it to now.  Just overlooked the manner 
of declaring abstract types and it's relation with what I was trying to 
accomplish.

>>I think it is because you have not given a mode for method draw/3 in the
>>typeclass declaration in shape.m.  You should try adding the line
>>    mode draw(in, di, uo) is det,

That was the part I was trying to figure out.  I couldn't figure out how to 
get the mode declared, and didn't think to put both the predicate and mode 
within the typeclass definition.  Works like a charm when you declare the 
mode.  I end up with typeclasses that look like:

:- typeclass shapeClass(This) where [
   func getX(This) = int,
   mode getX(in) = out is det,

   ..... cut .....

   pred draw(This, io__state, io__state),
   mode draw(in, di, uo) is det
].

+++++++++

Ok.  I'm good to go with the problems I had.  One additional question I have 
concerns the write accessors.  In my example, the read accessors work fine in 
the form of:

   getX_Circle(This) = This^x.

I gather also that there is a write form of the accessor using the ':=' for 
the assignment but I can't figure out how to use it.  Currently I'm writing 
it as:

   setX_Circle(circleRecord(_,Y,Radius), X) = circleRecord(X, Y, Radius).

which spins off a new record based on the values in the input record.  
Anyhow, is there a way to use the set accessor to make this shorter, if not 
more efficient?

Thanks.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20010424/e42b79c9/attachment.html>


More information about the users mailing list