Hacker News new | past | comments | ask | show | jobs | submit login

I've heard of it but not used. Being able to say: "(name :type string)" looks great.

Does it mean the system gives me an error if a method is called with a number where a string was declared to be the argument type?




At runtime every CLOS implementation will do that. Note that it allows for more than one parameter to be specialized. In the example FOO is using multiple dispatch with two parameters P and S. When we call FOO with arguments for which no method exists we get:

  CL-USER 22 > (defclass person ()
                 ((name        :type string)
                  (age         :type (integer 0 150))
                  (exployed-by :type company)))
  #<STANDARD-CLASS PERSON 4210214F63>

  CL-USER 23 > (defmethod foo ((p person) (s string))
                 (list p s))
  #<STANDARD-METHOD FOO NIL (PERSON STRING) 402025C653>

  CL-USER 24 > (foo (make-instance 'person) 3)

  Error: No applicable methods for #<STANDARD-GENERIC-FUNCTION FOO 4060000B5C> with args (#<PERSON 402025F1DB> 3)
    1 (continue) Call #<STANDARD-GENERIC-FUNCTION FOO 4060000B5C> again
    2 (abort) Return to top loop level 0.

  Type :b for backtrace or :c <option number> to proceed.
  Type :bug-form "<subject>" for a bug report template or :? for other options.

  CL-USER 25 : 1 >


Note to galaxylogic: When lispm wrote:

     (defmethod foo ((p person) (s string))
                     (list p s))
He was implicitly defining a "generic function" "foo" of two arguments.

Defmethod was defining an implementation of the generic function foo for arguments person with string.

One could later put something like:

     (defmethod foo ((p person) (p person))
                     (send-greeting p p))
And thus define a "foo" that works on two persons, etc.

CLOS allows specializing not just on classes, but also on specific instances, so some method applies only for a specific instance. For example a "foo" that applies only to a particular "important" person.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: