I found it painful to do object-oriented programming in Common Lisp. The main problem is the naming of methods.Please see the code below.
(defclass foo () ())
(defmethod action ((object foo))
(write-line "hello"))
(defclass bar () ())
(defmethod action ((object bar) boo)
t)
(print
(action (make-instance 'foo)))
(print
(action (make-instance 'bar) 1))
The two action
methods are not different implementations for a same generic function. They have the same name by accident.
But Common Lisp requires all methods with the same name to have the same numbers of parameters.
In order to avoid the name conflicts, I usually prefix the methods' names with the class names, such as foo-action
and bar-action
. But that causes the code to be quite verbose in real program, e.g. (lengthy-class-name-do-something some-variable)
.
Other object-oriented programming languages, like C++ and Java, do not have such problems. You can write that like some_variable.do_something()
with no name conflicts in them.
So I'd like to know if there exists any better solution to the problem described above?
The key difference is indeed that methods are methods on generic functions. They are not methods on classes.
This may sound like words for words' sake. But it should highly influence your naming choices. "Action" is a pretty bland name for a generic function. When foo
acts, what does it actually do? When bar
acts, what does it actually do?
If action
truly is the best name, would having a foo:action
and a bar:action
make sense (that is, an action
in the foo
package, and one in the bar
package)? They are different GF's. With, of course, the drawback that it is no longer as easy as just calling action
on them.
And that hints towards "you don't need a method, you can just use a function", because you don't need a method to do things with classes in CL.