New Groovy --- ClosureBlock problem

xiaoxiao2021-03-06  40

After experiencing the hurricane from Blog to Mailing List, New Groovy

Roadmap powder debut. To a certain extent this is a lot of dust in the world.

Response of Groovy Is Dead. On the other hand, Groovy's bitter Mike Spille's response to the new Groovy characteristic criticism is

Determine the document in Wiki form.

All new features are the greatest, of course, this article: Return / Break / Continue To Behave Inside Closures Like Sstatements Work In Other Blocks (Such as the block on a for () or while () loop.

In short, this is unified Block and Closure.

Here I couldn't help but sell a melon. In fact, I have already felt this taste. I was on January 16 in Groovy-dev, RE Mike Spille:

I think we shop regard foo () {} as syntax-sugar, this Feature Eliminate The Difference Between Built-in Flow Structure and user-defined Method Call.

IF = {condition, dosth | if (condition) dosth ()}

T = {Println 'Hello!'}

IF (true) {println 'Hello!'} // built-inix

IF (True) {Println 'Hello!'} // user-defined if is similar to

Built-in If, That's Groovy!

IF (True, T) // Normal Method Call

IF (True, {Println 'Hello!'} // Normal Method Call Even the Last

Param is a anonymous closure

>

> The Puzzling Erroors Bit Is Also Why I Favor a Keyword for Closures. It'll

> NOT ONLY GREATLIFY THE Grammar and Parser, But I Think It's Better

> for users Too. It Clearly Signals Where Closures Are IN Your Code, And

> Avoids The Problems of_Users_ not knowing when Have a closure and

> When the Have a block.

I Think The IS No Essential Difference Between Closure and Block. User Have A Block, The Want To Reuse The Block. Make the block can be passed or returned or parameterized --- That Is Closure.

It seems that I have been close to the idea of ​​getting close to the anonymous function in JavaScript, which is just closure. I have confirmed this Closure and the subtle relationship between general block.

Closure's Syntax largely eliminates the difference between built-in process structure and user definition method. In other words, we can easily write a method of accepting a closure parameter, making it calling syntax to very similar While, WHEN (without ELSE IF) syntax structure! Although it is still unable to copy for (;;) and IF-ELSE special syntax structure, this is already very shocking. Furthermore, I found that there is no essential difference from the application programmer's point of view, and there is no essential difference in BLOCK and Closure. Block can be seen as a code segment, and Closure is a code segment that can be reused (deliverable, returning, and parameterized).

However, Closure and Traditional Flow control have certain conflicts, mainly in how to achieve Break, Continue semantics. The previous recommendations are generally requested to return a specific constant tag such as Closure.Break to indicate, and then handle this flag by the caller of the Closure.

A larger problem is that the same as the code function abstraction and multiplexing units, Closure has conflicted with Function (Method). This is the Return issue!

Obviously, according to the general idea, Closure is a Function or Method, anonymous closure shape with the simple construction of {param | expressions} with Block, can be regarded as a syntax SUGAR. And from the initial until now, this sugar is mainly to achieve the brief gpath's simpleness. And on the other hand, people who have a little entry knowledge from JS or to the Florus programming (FP), Closure should be equal to FUNCTION.

It should be said that the same is true at the beginning of Groovy. The only thing is that the simplicity of GPath is used, and the design that can omit RETURN (directly returning the value of the last statement).

Since then, in Groovy's CodeBase, all Closures has almost no return keywords. Because there is almost no necessary, there seems to be no reason to remember it even refer to it. From this, a door opened ...

Consider a traditional program. This function checks each row of files named file and returns the first line number that satisfies the Filter, otherwise returns -1.

DEF FINDLINE (File, Filter) {

Lineno = 0

f = new file (file)

While (! f.iseof ()) {

Line = f.readline ()

IF (Filter (Line) Return Lineno;

ELSE Lineno

}

Return -1

}

Now a newbie just looks like a few closure, excited, so exchange with Closure Style to write:

DEF FINDLINE (File, Filter) {

Lineno = 0

New file (file). Eachline {line |

IF (Filter (Line) RETURN LINENO; Else Lineno

}

Return -1

}

OK, everything looks right, the code is clearer to express the main purpose of the program, there is almost no extra thing.

However, unfortunately, this program cannot reach the expected effect in Classic Groovy, and Findline always returns -1!

Because return in anonymous closure after Eachline is back from the CLASURE to the call point inside the Eachline method (in fact the Eachline method is just simple to ignore this return value, no one is looking forward to it), not as programmers Want to return to Findline calls! OK, this is a programmer's question, misuse Return - Some people will conclusions. How to reach this feature in the context of the suggestion, just considering how intuvice, and Groovy's goal is to let the programmer can act according to intuition (the troubled procedure ends; Allow cross-line, that is, the examples are not easy to blame the programmer.

As an example, in this structure, Return's usage is very clear, without prejudice, any Java programmer will almost write such a program after turning to Groovy. Since Closure is used to simplify the iterative structure (Martin Flower "is therefore so much in the language without Closure), for programmers who turn over from C-Style, facing the most commonly used EACH, naturally It looks like a substitute for For, WHILE structures, so regards returnes to return from Findline, instead of returning from anonymous closure.

For this issue, Mike Spille's opinions are: Everything is really like Block, and programmers have forgotten that actually is a speech sugar, essence is a Method is not a block, and thus induces programmer's expectation of the programmer. Return position. In this case, we should be clear where block is Closure. Its suggestion is to add a keyword indicating Closure, such as DEF {Closure Code ...}

In this regard, some people pointed out that it is better to go with C # delegate! (On the other hand, that is also the actual situation of JS, the function keyword is equivalent to this keyword)

In any case, the situation is clear: Ordinary block and closure are so similar to the Syntax, the solution is not resolved, distinguishes them (Mike Spille's recommendations), or unify them - make Break / return, etc. Semantics in Block Consistent with Closure!

John Rose proposed and for most people accept the case that closure's Return is the most desirable behavior of programmers: returns to the function defined. And he also referred to SmallTalk, Ruby, Lisp et al.,,,,,,,,,,,,,,,,,,,, Thus, by using the syntax structure of the CONTINUE L: Value in Closure, the need to return to Value from Label L can be reached. (Of course, the opportunity to use this weird Continue is actually very few, according to my consistent tone - you can of course use XXX, but if you need to use xxx, it is often a bad smell signal ...)

For this proposal, Mike Spille nature is naturally opposed, and the accusation of this is also involved in the development schedule, management model and other issues. If I don't miss the wrong, Mike Spille's Rant (although he does not admit) or even asked several core characters to step down ...: D

By the way, when I wrote the Mail in front, I didn't realize this problem. John Wilson's RE pointed out this tricky question: On 16 Jan 2005, At 13:53, Shijun He Wrote:

> I Think The Is No Essential Difference Between Closure and Block.

> User Have A Block, Then Want to Reuse The Block. Make the block

> Passed Or Returned or Parameterized --- That Is Closure.

>

This is almost true but not quite ..

IF (a == 1) {

Return // Returns from Enclosing Function

}

a.each {

Return // Returns from Closure

}

THIS (AND Break in a closure) Catches People Out A LOT.

THIS HAS BEEN Discussed a Great Deal But We Have Not Come Up with a

Solution Which pleses Everybody.

Obviously, to be pleases everybody is Mission Impossible: (in fact noisy sky turned over ... Mike Spille has been crowned in the name of FUD.

In recent days, I also discussed this issue with PT comrades and also pulled Ruby and Lisp / Scheme to "find roots". The opinions of comrades of PT are uniformly consistent with Mike Spille. PT insists that Closure should be function, changing the semantics of Return is unacceptable. However, the status quo of Ruby is closer to John Rose (at least anonymous closure returns to the upper layer function). If we don't doubt John Rose's professional standard (this is also Mike Spille, John is a master with multiple language experience), then the Closure of this Return semantic, is the language standard of SmallTalk, and Ruby has A lot of concept is the teacher from SmallTalk, and Closure should no exception. Further, even the main designer James Strasen, the main designer of Groovy has been spoken: What we have to change, but it is necessary to clear the semantics of return / break / continue that never explicitly, it is necessary to complete the Closure function that is not fully implemented. . (I am talking about: This is a horse behind :)).

The problem finally entered the "philosophy" phase (please don't mistaken, I am in ridicule philosophy). That is "what is closure". Obviously, I am more believed to believe in John Rose, and the Closure since Lisp is the case, namely a special Block structure. Even MIKE SPLLE is also said that the goal of Groovy is Java programmers, should not be able to "west", should first consider the accepted level of Java programmers, such as Closure / Block's unified understanding of Java program structure The foundation. (But at this point after I saw the closure 'of' Java, I couldn't convince me at all.)

转载请注明原文地址:https://www.9cbs.com/read-61500.html

New Post(0)