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?