[m-users.] typeclass, class and list confusion

Sean Charles (emacstheviking) objitsu at gmail.com
Tue Apr 5 06:01:57 AEST 2022


Hi,

I think I know why I failed but in the manual, given these:

:- type thing  --->    empty_thing    ;       thing_with_label(string).
:- type blob   --->    empty_blob    ;       blob_with_label(string).

:- instance showable(thing) where [ ... ].
:- instance showable(blob) where [ ... ].

I then tried to do this and got an error message which I was half-expecting. The problem is, I only half expected it!

    P = (pred(X::in, !.IO::di, !:IO::uo) is det :-
        io.format("a thing: %s\n", [s(show(X))], !IO)
    ),
    list.foldl(P,
        [
            empty_thing,
            thing_with_label("Seems restricted to underlying type"),
            empty_blob,
            blob_with_label("Seems restricted to underlying type")
        ],
        !IO).

I have obviously mis-understood what I can do with a typeclass. So basically then, a list must be homogeneous by type, and providing a typeclass to group disparate types is not permissible because. I can see that from the code, I mean, it's obvious that there are two different types of things in the list but I somehow thought that it might work.

So, if I understand it correctly then, it failed because despite the typeclass forming a common API, namely `show`, this has absolutely no bearing on the fact that I still tried to create a non-homogeneous list ? I'd love to have a sensible interpretation of my half-brain attempt to do the above!

Thanks,
Sean.

PS. Not sure if it's my imagination, but I upgraded to 20.01 and on my 10 year old iMac, compilation feels quicker, only by a shade, but maybe that's just me!

:)


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


More information about the users mailing list