#266 Return type conventions

tompalmer Thu 26 Jun 2008

Why return This from normally Void methods when "with" blocks already provide a convenient calling mechanism? Just curious.

andy Thu 26 Jun 2008

Returning This is used for method chaining, and is independent of with blocks:

g.fillRect(x,y,w,h).drawRect(x,y,w,h)       // no with block
g { fillRect(x,y,w,h).drawRect(x,y,w,h) }   // with block

Though given color and font are fields, in this context, its of arguable use.

tompalmer Fri 27 Jun 2008

What I mean is that I always thought of call chaining on this as a workaround for languages without "with" blocks. For example, I think the following is clearer than either example above, and it allows the natural Void return type:

g { fillRect(x,y,w,h); drawRect(x,y,w,h) } // with block, Void returns

brian Fri 27 Jun 2008

They actually both solve the problem. Although I think chaining is more natural in a lot of cases, especially when returning a covariant result:

t := MyThread.make.start  // t is typed as MyThread, not Thread

Although you could just as easily write this and get the same typing:

t := MyThread.make { start }

I hadn't really thought of that until just now, but then again with-blocks are a really new way of thinking for me. Though I'm not sure I would get rid of This returns, and I think convention should be to use chaining when it works.

tompalmer Fri 27 Jun 2008

Well, make (or new if we switch to that) has an obvious type. Chaining there is no big deal. I think other uses for chaining are less obvious in their meaning (unless you know the API already), even though you can sort of get the drift from context.

Login or Signup to reply.