Vinn
Vinn

Reputation: 1181

importing specific symbols common lisp

I often use package symbols like this:

(ql:quickload :cl-json)


(cl-json:encode-json-to-string .... )

I would like to use it like this without the cl-json part:

(defpackage custom-sender
  ;; import the exactly the symbols I need from a package
  (:import-from :cl-json :encode-json-to-string)
  (:use :cl))

(encode-json-to-string .....)

As you can see, I tried to use the import-from to get this single symbol.

However, I still get the error that encode-json-to-string is undefined.

Upvotes: 1

Views: 249

Answers (1)

Rainer Joswig
Rainer Joswig

Reputation: 139251

DEFPACKAGE defines a package. It does not set any scope itself.

If you want to set the package scope, then using IN-PACKAGE is a way. You have imported the symbol into a specific package, then you need to make that package current to see the effects.

Just loading something may not set a specific package you would expect.

Remember:

An ASDF system might define zero or more packages. system and packages are two different and orthogonal concepts in Common Lisp.

An ASDF (a popular build tool) system is a tool specific name for a library / some piece of software. This can be compiled, loaded, etc.

A Common Lisp package is a namespace for symbols. It's a standard feature of the language. packages know nothing about systems.

In some programming language (say, Java) a class might be defined in one file and also be a namespace. In Common Lisp are files, namespaces, modules/systems, ... all independent concepts. It's entirely possible to define a library incl. its n packages in one file. Usually people will organize their software into a bunch of files, one or more packages and one or more systems. systems then are the granularity of a collection of files, which together can be compiled/loaded.

Upvotes: 3

Related Questions