Re: Refactoring to a Closure

Adam Sroka <[email protected]>
Newsgroups gmane.comp.programming.refactoring
Message-ID <[email protected]>
Actually most uses of anonymous inners are not closures. To be a closure, as
opposed to a lambda or anonymous function, they would need to capture some
state from the outer class. It is possible to use anonymous inner classes
that way, but it is rarely done.

One place where it is done is in JMock 2.x where a static initializer on an
anonymous inner is used to set expectations on the mock in the surrounding
class, and the expectation is later invoked to verify itself (by the
framework.) The JMock folks really wanted closures (Continuations, even) to
do this, but they found a hackish way to do it with inner classes. I do
think it is a kludge (The syntax is, AFAIK, never used anywhere else in the
known universe) but it is a good example of something that could be done
with closures if Java supported them.

On Sat, Dec 19, 2009 at 5:04 AM, twitter.com/nfma <
[email protected]> wrote:

>
>
> That has been my experience, to have to use the more cumbersome anonymous
> inner class where a closure would be a perfect solution.
>
> And refactoring to closure would be a very handy refactoring to have for
> all
> those verbose anonymous inner classes I have to use. :)
>
> 2009/12/19 Mirko Stocker <[email protected] <me%40misto.ch>>
>
>
> >
> >
> > Hi!
> >
> >
> > On Friday 18 December 2009 22:48:42 jcsextro wrote:
> > > I won't write any code using closures because a closure isn't the
> > simplest
> > > thing that can work. I know this because of the millions of bits of
> code
> > > that I have already written in Java without closures.
> >
> > But, if closures weren't even available in the languages you used, no
> > wonder
> > you haven't had the chance to use them :-) How many times have you used
> an
> > anonymous class in Java? I'd bet that in most cases, passing a simple
> > closure
> > would have done the job as well, and with much less code to write.
> >
> > I'd recommend to take a look at Scala to see how Java with closures could
> > look
> > like.
> >
> > Regards
> >
> > Mirko
> >
> >
> >
>
> [Non-text portions of this message have been removed]
>
>  
>


[Non-text portions of this message have been removed]



------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/refactoring/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/refactoring/join
    (Yahoo! ID required)

<*> To change settings via email:
    [email protected] 
    [email protected]

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.