3 ms·
>I was just curious how "generic functions" in particular could help in defining types and interfaces. I was not asking for the whole story about the whole of C
by flavio81 8y ago
>I was just curious how "generic functions" in particular could help in defining types and interfaces. I was not asking for the whole story about the whole of CLOS.
I understand.
For something similar like a Java interface, you could define generic functions for each method your interface declares. For example consider interface IStream with methods open(), close(), sendbytes(b) and readbytes(b)
In Lisp you could declare the following generic functions: open(x), close(x), send(a, b), and read(a,b).
Then you simply define methods for any kind of (concrete) stream. Moreover, "send" and "read" could be defined to send/read not just bytes, but also any other kind of object. Or you could also define a "send" and "read" that works from stream to stream. For the latter, on java you would have to either redefine IStream, extend IStream, or create another class specialized on copying from Stream to Stream. On Lisp you just define an additional method for the generic function send(a,b) where a and b are both streams.
The CLOS system will take care of calling the correct method. If there isn't a correct method this would signal an error: "No applicable method".