james
7/24/2015 1:32:00 AM
On Tuesday, July 21, 2015 at 9:53:53 PM UTC+8, Kaz Kylheku wrote:
> On 2015-07-21, james <dinglei2008@gmail.com> wrote:
> > I just find a interesting thing in lisp which is string-designator.
> >
> > Here is my test code:
> > (defpackage :a.com (:use :cl)(:export :add))
> >
> > (in-packate :a.com) works
> > (in-package "A.COM") also works.
> >
> > I find the in-package will take string-designator parameter. So I understand
> > the string-designator's usage. It will accept string,char,symbol as a
> > parameter. Which is like a super-class type in java. The benefit is that the
> > compiler will do parameter check. Did my understood right?
>
> The obvious benefit is that you can use a symbol or string when referring
> to a package.
>
> Note that the package system *underlies* the symbol system, and so it would be
> somewhat inappropriate to require packages to be designated by symbols.
>
> > Also, how could I define a function which will use string-designator as a
> > parameter? Is that possible to create designator for user create class?
>
> "string-designator" is not a type; it's a convention. Some symbol-related
> API's in Lisp can take a string or a symbol.
>
> For instance, API's that refer to a package can accept its string name,
> or a symbol in the place of a string.
>
> This doesn't mean that there exists a designator class type.
>
> In Lisp, we can easily make completely unrelated types provide the same (or
> closely-related) functionality. We can do this in using Lisp's object system
> via generic functions and methods, or via explicit type tests.
>
> ;; use typecase:
>
> (defun myfunc (whatever)
> (typecase whatever
> (string ... code for string case ...)
> (symbol ... code for symbol case ...)
> (t (error ...))))
>
>
> ;; use CLOS:
>
> (defgeneric myfunc (whatever))
>
> (defmethod myfunc ((whatever string))
> ;; ... code for string case ...
> )
>
> (defmethod myfunc ((whatever symbol))
> ;; ... code for symbol case: e.g. recurse to string case:
> (myfunc (symbol-name symbol)))
>
> ;; other types handled implicitly via "no applicable method" error.
> ;; or else we can do:
>
> (defmethod myfunc (whatever) ;; same as ((whatever t))
> (error "myfunc: ... error message here "))
Thanks. My feeling is there are so many unknown details for me in learning lisp but interesting. Thanks for such a good community here.