Programming Clojure, Third Edition: Invalid token: ::music/id spec error (122)

Hi @alexmiller

I’ve been following along through PC 3rd, and hit a bump on page 122 - the ‘name’ format for the spec seems incorrect. I went back and it looked like adding a namespace might fix it, but that didn’t help either, I still get the ‘invalid token’ error.

Code:
(ns net.solarwinter
(:require [clojure.spec.alpha :as s]))

(s/def ::music/id uuid?)

Evaluating this produces:

  1. Unhandled clojure.lang.Compiler$CompilerException
    Error compiling spec.clj at (4:18)
    #:clojure.error{:phase :read-source,
    :line 4,
    :column 18,
    :source
    “/Users/paul/Documents/Programming/clojure/spec.clj”}
    Compiler.java: 7647 clojure.lang.Compiler/load
    REPL: 1 user/eval7690
    REPL: 1 user/eval7690
    Compiler.java: 7181 clojure.lang.Compiler/eval
    Compiler.java: 7136 clojure.lang.Compiler/eval
    core.clj: 3202 clojure.core/eval
    core.clj: 3198 clojure.core/eval
    interruptible_eval.clj: 87 nrepl.middleware.interruptible-eval/evaluate/fn/fn
    AFn.java: 152 clojure.lang.AFn/applyToHelper
    AFn.java: 144 clojure.lang.AFn/applyTo
    core.clj: 667 clojure.core/apply
    core.clj: 1977 clojure.core/with-bindings*
    core.clj: 1977 clojure.core/with-bindings*
    RestFn.java: 425 clojure.lang.RestFn/invoke
    interruptible_eval.clj: 87 nrepl.middleware.interruptible-eval/evaluate/fn
    main.clj: 437 clojure.main/repl/read-eval-print/fn
    main.clj: 437 clojure.main/repl/read-eval-print
    main.clj: 458 clojure.main/repl/fn
    main.clj: 458 clojure.main/repl
    main.clj: 368 clojure.main/repl
    RestFn.java: 1523 clojure.lang.RestFn/invoke
    interruptible_eval.clj: 84 nrepl.middleware.interruptible-eval/evaluate
    interruptible_eval.clj: 56 nrepl.middleware.interruptible-eval/evaluate
    interruptible_eval.clj: 152 nrepl.middleware.interruptible-eval/interruptible-eval/fn/fn
    AFn.java: 22 clojure.lang.AFn/run
    session.clj: 218 nrepl.middleware.session/session-exec/main-loop/fn
    session.clj: 217 nrepl.middleware.session/session-exec/main-loop
    AFn.java: 22 clojure.lang.AFn/run
    Thread.java: 833 java.lang.Thread/run

  2. Caused by java.lang.RuntimeException
    Invalid token: ::music/id

I’m on Clojure 1.10.3 (java version “13.0.1” 2019-10-15, macOS Monterey, but I don’t think those should matter).

I think this is a bug in the book in that the music alias is not defined before it’s used as a qualifier. To do so, you either need to define a namespace music that is loadable or, more commonly alias music to some longer namespace (which is also loadable):

(create-ns 'domain.music)
(alias 'music 'domain.music)

In Clojure 1.11, the new :as-alias clause in require allows you to do this without loading any namespace:

(require '[music :as-alias music])

1 Like

Hi Alex

Thanks for the prompt reply, much appreciated - I was wondering what I was going to do about it. :slight_smile:

The create-ns/alias pair has fixed it for Clojure 1.10.3 - I’ll try 1.11 when I get chance, as the new clause does look simpler.

Thank you!
Paul