I'm learning about generic functions in CLOS.
Because of the type of examples I find in textbooks and online, I'm getting very confused. The examples always use the fact that there is multiple dispatch. Based on the argument type, a different calculation is performed. However, why are the arguments themselves never used in the examples?
Example code from Wikipedia
; declare the common argument structure prototype
(defgeneric f (x y))
; define an implementation for (f integer t), where t matches all types
(defmethod f ((x integer) y) 1)
(f 1 2.0) => 1
; define an implementation for (f integer real)
(defmethod f ((x integer) (y real)) 2)
(f 1 2.0) => 2 ; dispatch changed at runtime
In the examples above, you can see the methods themselves never actually use the x
or y
variables. Is it a coincidence that all of these examples never use the variables? Can they be used?
Also, it's written on Wikipedia:
Methods are defined separately from classes, and they have no special access (e.g. "this", "self", or "protected") to class slots.
Alright, so methods do not have a "this" because they do not belong to a class. But why can generic-function methods have a receiver then? Isn't the receiver similar to the 'this' in a class?
Sure you can access the variables from the parameter list. The wikipedia example is only there to illustrate which method returns the value.
CLOS generic functions don't have a single receiver, thus it makes no sense to use the word receiver. The implementation you mention likely does not implement full CLOS, but a variant without multiple dispatch.
CLOS Example:
You see that the both variables
s1
ands2
are used. It just makes no sense to name one of themreceiver
.But you can name the variables as you like and when your application only uses dispatch over the first argument, you might want to call that variable
receiver
, but the name has no semantic for CLOS. It is just another name.Generally for CLOS code it is preferred to give the arguments useful names.
This is misleading, because we are not doing message passing in CLOS:
This is more useful: