Search code examples
lispcommon-lisppackageslimesbcl

Import package into SLIME REPL


I'm trying to import some functions from SBCL non-standard builtins to use with a socket. When I do this outside slime, with bare interactive shell + SBCL it works, but not in SLIME.

What I did:

(import 'sb-bsd-sockets:host-ent-address)
(import 'sb-bsd-sockets:get-host-by-name)

or

(use-package :sb-bsd-sockets)

After doing either one, SLIME greets me with an error, saying that I'm trying to import something already in cl-user package. This is a correctable error, which I proceed to correct by choosing the symbols found in sb-bsd-sockets package. No further errors follow.

Then, when I try to compile function:

(defun nslookup (hostname) 
  (if hostname 
      (sb-bsd-sockets:host-ent-address (sb-bsd-sockets:get-host-by-name hostname)) 
      nil))

It works. But if I try to compile this:

(defun nslookup-1 (hostname) 
  (if hostname 
      (host-ent-address (get-host-by-name hostname)) 
      nil))

Then I get a warning about undefined functions, and an error when I try to call nslookup-1.

To my surprise, if I try doing this in REPL:

CL-USER> #'host-ent-address
#<STANDARD-GENERIC-FUNCTION HOST-ENT-ADDRESS (1)>
CL-USER> 

It "works". I.e. it knows the function, but decides not to use it...

There must be some (special?) way to either import packages into SLIME's REPL, or at least symbols from those packages, otherwise it's very inconvenient to use long names for testing and then to replace them for the actual program...


Solution

  • If by "correctable error", you mean "name collision", that's an issue you can handle by defining your own packages and using the :shadow and :shadowing-import-from clauses. Check Chapter 21 of PCL for details.

    Short answer: if you're trying to import a new symbol that's already defined in your current package, you need to do shadowing-import instead of the standard import (though you should really be using the corresponding clauses to defpackage rather than calling those yourself).

    For part two, what do you mean by "compile the function"? When I evaluate that second definition of nslookup-1 after shadowing-importing the two pieces from :sb-bsd-sockets, it works fine. Are you sure you're trying to evaluate it in a package where you've already imported the appropriate functions?