On Thu, Dec 16, 2010 at 2:56 PM, Michael <mw10...@gmail.com> wrote:
>
> I'm trying to use << from clojure.contrib.strint perform string
> interpolation in a string variable.  The following,
>
> (ns strint-test (:use clojure.contrib.strint))
>
> (def v 1)
> (println (<< "v: ~{v}"))
>
> (def s "v: ~{v}")
> (println (<< (str s)))
> (println (<< s))
> results in
>
> v: 1
> v: ~{v}
> java.lang.RuntimeException: java.lang.RuntimeException:
> java.lang.IllegalArgumentException: No matching method found: indexOf
> for class clojure.lang.Symbol (strint-test.clj:8)
>
> Does anybody have any advice on getting (<< s) to work?  I have
> included the << macro and associated function from
> clojure.contrib.strint for reference.  Thanks.

The macro interprets its string argument at macroexpansion time. If it
is a string literal, everything works fine. But if it's a symbol or
s-expression, things go wrong, as you saw. In the case of (<< (str s))
the macro gets a list of 'str and 's rather than "v: ~{v}". In the
case of (<< s) it gets a symbol 's. The latter in particular causes
the .indexOf not found in clojure.lang.Symbol exception you saw.

Frankly, I'm not sure what the use of this is. Compare:

(println (<< "v: ~{v}"))
(println (str "v: " v))

The latter is actually shorter. And the << macro as you've seen won't
work with a runtime-variable format; whereas (apply str some-seq) lets
you vary the content and order at runtime, and the printf-like format
function allows a C-like format syntax where the format string can
again vary at runtime.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to