On Oct 15, 11:47 pm, "Stephen C. Gilardi" <[EMAIL PROTECTED]> wrote:
> Hi Parth,
>
> > But if I do this in a doto, it doesn't seem to work but
> > I don't get any error message.
>
> > user=> (doto (new java.lang.String "hello") (toUpperCase))
> > "hello"
> > user=> (class (new java.lang.String))
> > #=java.lang.String
>
> > Shouldn't this be working? If this is (by design) because strings are
> > immutable (so no doto) shouldn't there be an error message?
>
> > What am I missing here.
>
> "doto" operates on an object and returns the object that it operated
> on. In your case, it's the original string that gets returned. Strings
> are immutable so the toUpperCase call created a new string which was
> then thrown away.
>
> The "->" operator will accomplish what you're looking for here. It
> returns the result of the operations, each result becoming the input
> for the next::
>
> user=> (-> (String. "hello") .toUpperCase)
> "HELLO"
>
Another semantic marker here is 'do'. do in Clojure implies side-
effects. Since you can't uppercase a string by side effect, doto isn't
the right tool for this job.
doto can't throw an error here or elsewhere when used on an immutable
object because:
a) It can't generally know which objects are immutable
b) Even immutable objects may have methods with other side-effects
Rich
--~--~---------~--~----~------------~-------~--~----~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---