Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I understand that, internally, everything is of some type.

What Lisp is lacking though is type declarations for variables, method parameters and returned values. These make codebases immensely more readable.



Common Lisp added type declarations in 1984.

A usual type declaration looks like this:

    (defun month-name (month)
      (declare (type (integer 1 12) month))
      (the string
           (ecase month
             (1 "Jan") (2 "Feb") (3 "Mar") (4 "Apr") (5 "May") (6 "Jun")
             (7 "Jul") (8 "Aug") (9 "Sep") (10 "Oct") (11 "Nov") (12 "Dez"))))
You can also declare the type of the function, outside of the function:

    (declaim (ftype (function ((integer 1 12))    ; the parameter list
                              string)             ; the return value
                    month-name))                  ; the function name
CLOS (the Common Lisp Object System) was added a few years later.

    CL-USER 1 > (defmethod add ((a integer) (s string))
                  (+ a (parse-integer s)))
    #<STANDARD-METHOD ADD NIL (INTEGER STRING) 402005B1C3>

    CL-USER 2 > (add 12 "23")
    35
What do you think the words 'integer' and 'string" are about?


I love it how we can have lisp code in just about any thread here :)


I didn't know you could do that in CL, thanks!




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

Search: