groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From alessio <>
Subject Re: "External" closures, why?
Date Tue, 01 Dec 2015 20:39:25 GMT
On Tue, Dec 1, 2015 at 9:24 PM, Winnebeck, Jason
<> wrote:
> What was recently added is that you don't need to explicitly cast a closure to a SAM
(single abstract method), which was adding a feature to match Java 8 lambdas (that also don't
need a cast). So if you have a method taking an SAM, you no longer need to explicitly cast:
> interface X { void xf() }
> void func(X x) { x.xf() }
> func { println "Hello, World!" }
> It used to be before Groovy 2.2 that you had to call func this way:
> func({ println "Hello, World!" } as X)

Thanks, I am afraid I am getting lost though. Jochen's code from
before (which ran just fine on 2.0) didnt have an explicit cast
either. So why would I need one here, but not in his example

  def bar(shouldCall, toBeCalled) {
    if (shouldCall) return toBeCalled() else return null
  } // the returns are actually optional

  assert bar(true) {1} == 1
  assert bar(true, {1}) == 1 //alternative syntax

The only difference I can spot is that he did not specify an explicit
type in his declaration. Is that the reason for the explicit cast

> Closures are the closest equivalent to Java lambdas, but they do more than lambdas because
they support delegation (this is similar to setting "this" in JavaScript closures, except
in Groovy you detain both the delegate and the original "this" -- as the "owner" field). Closures
can also be queried for the number of arguments they take, allowing overloading of closures
that I don't believe is possible with Java 8 lambdas. And of course with Groovy closures work
back to Java 5, so if you can't use Java 8 yet, you can use Groovy.

I have already read about the owner and the delegate, have to admit
though that it is not yet fully clear (but then I just started reading
today :) ).

My main question is admittedly though less about the overall concept
but the fact that you can pass a function outside of the method call.

My apologies for dwelling on this subject, but I am still baffled by
this and find it quite confusing in terms of readability (maybe just
my personal view :) ). Why would the syntax allow for an argument to
be outside of the call? Maybe somebody could explain the "historical"
reason or an actual case where that made sense.


View raw message