3

How do namespaces work in Chicken Scheme? I am now using the parley egg, and when I define a function with the name e.g. read, that causes an error because of name clashing (actually, because my read overwrites parley's own read, and it is invoked with wrong type.

Here's the code:

(use parley)

(define (read p) p) ; This `read` function conflicts.

(let loop ((l (parley "> ")))
  (if (or (eof-object? l)
          (equal? l "quit"))
    (print "bye!")
    (begin
      (printf "you typed: ~s~%" l)
      (loop (parley "> ")))))

How can I avoid collisions like these?

UPDATE

I've reduced the code necessary to reproduce this:

(use parley)
(define (read p) p)

this gets the following error: Error: illegal non-atomic object: #<input port "readline.scm">

Obviously, my read function is clashing with parley read. But I don't know how to avoid this without renaming my function.

Sergi Mansilla
  • 12,495
  • 10
  • 39
  • 48

2 Answers2

5

According to the documentation you can use the same tricks as when you import modules in modules. You then have lots of options, like prefix:

(use (prefix parley parley:)) ; all imported symbols have been prefixed with "parley:"
wasamasa
  • 310
  • 2
  • 9
Sylwester
  • 47,942
  • 4
  • 47
  • 79
1

It was not that obvious. It turns out that read is an essential function in Scheme that converts external representations of Scheme objects into the objects themselves. Overwriting it is not a good idea, because users of my library or app probably assume that read is not overwritten, and try to use it as a parser.

But Chicken Scheme should be giving a warning, not an error, so that's probably a bug in Chicken.It is to be determined if it is actually csi the one who is at fault, instead of Parley.

In any case, it's a terrible idea to overwrite read.

Sergi Mansilla
  • 12,495
  • 10
  • 39
  • 48
  • Indeed, R5RS allows redefinition of basically anything. If you want to have your own "read", the easiest way to do that is to [put your code inside a module](http://wiki.call-cc.org/man/4/Modules), which creates a namespace separate from the toplevel. This will avoid any unintended name clashes. This will also show a warning that you're redefining an existing procedure, so you can import the "scheme" module in your module without read. Alternatively you can rename or prefix the imported read like suggested by Sylwester. – sjamaan Jun 25 '15 at 06:56