« Return to Thread: something like Clojure's "doto"?

Re: something like Clojure's "doto"?

by gabriele renzi-5 :: Rate this Message:

Reply to Author | View in Thread

(sorry I originally replied only to andreas)

On Sat, Jul 11, 2009 at 11:36 AM, andreas s.<andreas_scheinert@...> wrote:
>
> Hello!
> This seems also impressive to me, now i try to see this construct in a
> broader context. So it's main purpose would be to encapsulate the
> instantiation  and method invocation with certain parameters in one
> construct. Am I missing something essential, else here?

I think it's main purpose is to change the self-object in a scope,
which can be used for pleasant DSLish effects :)
From what I understand this is akin to "with" in some languages
(pascal?), or instance_eval in ruby.

The first solutions (albeit cool) is slightly wrong in this sense, as
it forces you to use a receiver.

This:
 doto( new java.util.HashMap[String,Int] )( _.put("a",1) )
is no better than
 val x = new java.util.HashMap[String,Int]; x.put("a",1)

The second I guess would be the more correct but it forces client side
import. Ideally, one should be able to abstract it away, e.g.

doto(x) { //no additional boilerplate
 foo(1) // calls x.foo(1)
 bar(2) // calls x.bar(2)
}

and I'd love to know how to do that in scala :)

 « Return to Thread: something like Clojure's "doto"?