On Sat, Feb 12, 2011 at 9:35 PM, Ken Wesson <kwess...@gmail.com> wrote:
> I rarely have more questions than answers here, but this is one of
> those times, and it has to do with efficiency.
>
> Is the bytecode generated from
>
> (let [^StringBuilder sb (StringBuilder.)]
>  (.append sb "Hello, ")
>  (.append sb "world")
>  (.toString sb))
>
> equivalent to Java
>
> final StringBuilder sb = new StringBuilder();
> sb.append("Hello, ");
> sb.append("world");
> return sb.toString();
>
> or instead to
>
> final Object sb = new StringBuilder();
> ((StringBuilder)sb).append("Hello, ");
> ((StringBuilder)sb).append("world");
> return ((StringBuilder)sb).toString();
>
> ?

Frak. I tried a cleverish way to maybe answer that question from
within Clojure, basically by seeing if a deliberately bad assignment
threw on the assignment or only when the thing got used.

Specifically, I used this:

(let [^String k 3]
  (.length k))

The question is, is this:

String k = (String)(new Integer (3)); // throw happens here
return k.length();

or:

Object k = new Integer(3);
return ((String)k).length(); // throw happens here

?

In my sandbox .clj file which I use when I use when I need useful line
numbers on stack traces I had this as


188 (let [^String k 3]
189   (.length k))

but what I got when I ran it was

#<CompilerException java.lang.ClassCastException: java.lang.Integer
cannot be cast to java.lang.String (sbox.clj:3760)>

The last line of sbox.clj is a blank line directly below "(.length
k))" -- i.e., 190. Way less than 3760.

Seems like the compiler doesn't want me knowing where it really is
putting those casts! :)

-- 
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