How "closes" such a powerful abstraction that object systems and fundamental control structures are implemented with its help?

Here is a quote from scala programming chapter 1:

Closing is such a powerful abstraction that object systems and fundamental control structures are often implemented using them

Apparently, the statement does not apply specifically to Scala, but to closure in general, but I can’t make a big difference. Perhaps this is some kind of pearl of wisdom, intended only for those powerful compiler writers!

So who uses Closures to implement the basic management structures and why?

Edit: I remember reading something about groovy’s custom control structures using the syntax to close the last parameter of the method call and making the structure available to your code using metaclasses or use Categories keyword. Maybe this is related?

Edit: I found the following groovy structural element syntax link here (slide 38)

Custom Management Structures

Thanks to closing

  • When closing the latter, they can be "deduced" from parentheses surrounding parameters
  • unless(account.balance > 100.euros, { account.debit 100.euros })
  • unless(account.balance > 100.euros) { account.debit 100.euros }
  • Signature def unless(boolean b, Closure c)

Groovy's sentence seems to be syntactic sugar for creating custom Closure control structures, like the first-class control structures offered by the language itself.

+4
source share
4 answers

I commented on the case of management structures. Let me comment on the closure of objects. Consider what happens when you call an object method; it has access not only to the list of arguments, but also to the fields of the object. That is, the method / function is closed above the fields. This is no different from a bare function (i.e., not an object method) that closes variables in scope. However, object syntax provides a good mechanism for abstraction and modularity.

For example, I could write

 case class Welcome(message: String) { def greet(name: String) = println(message + ", " + name) } val w = Welcome("Hello") w.greet("Dean") 

vs.

 val message = "Hello" val greet = (name: String) => println(message + ", " + name) greet("Dean") 

Actually, in this example, I can remove the keyword "case" from the greeting so that the message does not become a field, but the value is still in scope:

 class Welcome2(message: String) { // removed "case" def greet(name: String) = println(message + ", " + name) } val w = new Welcome2("Hello") // added "new" w.greet("Dean") 

It still works! Now greet closes the value of the input parameter, not the field.

 var welcome = "Hello" val w2 = new Welcome2(welcome) w2.greet("Dean") // => "Hello, Dean" welcome = "Guten tag" w2.greet("Dean") // => "Hello, Dean" (even though "welcome" changed) 

But if a class refers to a variable in the outer scope directly,

 class Welcome3 { // removed "message" def greet(name: String) = println(welcome + ", " + name) // reference "welcome" } val w3 = new Welcome3 w3.greet("Dean") // => "Guten tag, Dean" welcome = "Buon giorno" w3.greet("Dean") // => "Buon giorno, Dean" 

Make sense?

+7
source

There are three main governance structures:

Sequence

 a = 1 b = 2 c = a + b 

Condition

 if (a != b) { c = a + b } else { c = a - b } 

Iterations / Loops

 for (a <- array) { println(a) } 

So, I assume that they imply that many languages ​​use restrictions for control structures (you can see the last two structures).

As an example:

 if (a < b) { for (i = a; a < b; a++) { println(i) c = i * i } } else { c = a - b } 

So, for is a closure inside an if closure, and else also a closure. This is how I understand it. They create a closure for the first if , if the condition is true, create a closure inside curly braces, call it. Then create a closure for the for loop and call it until the condition is true.

And I think that there is no list of languages ​​that use closure inside.

Update:

As an example, you can implement your own for loop in Scala (o is Cyrillic, so it will compile):

 def fr(start: Unit, condition: => Boolean, increment: => Unit)(body: => Unit): Unit = { if (condition) { body increment fr(0, condition, increment)(body) } } var i = 0 fr (i = 0, i < 1000, i += 1) { print(i + " ") } 

So actually this is how it can be implemented in other languages ​​internally.

+3
source

I would say that “closing is such a powerful abstraction ...” because, unlike standard methods, you have a reference to the calling object, regardless of the area in which the closing was called.

In Groovy, for example, you can add a new "capizeize" method to the String type:

 String.metaClass.capitalize = { delegate[0].upper() + delegate[1..-1].lower() } "hello".capitalize() // "Hello" 

Or you can do something more complex, like creating your own language (DSL) using closures.

 class ClosureProps { Map props = [:] ClosureProps(Closure c) { c.delegate = this // pass closure scope to "this" c.each{"$it"()} // iterate through closure, triggering missingMethod() } def methodMissing(String name, args) { props[name] = args.collect{it} // collect extracted closure properties } def propertyMissing(String name) { name } } 

Example

 class Team { // the closure static schema = { table team id teamID roster column:playerID, cascade:[update,delete] } } def c = new ClosureProps(Team.schema) println c.props.id // prints "teamID" 
+1
source

a) Before asking questions, try at least topics for searching the Internet.

b) After you have done this, ask specific questions.

c) Lexical closures are functions that have access to the lexical environment, inaccessible where they are called. Thus, their parameters can be used to select messages and pass parameters with these messages. For general management structures, they are insufficient if they cannot affect the call stack in sequential order.

-4
source

Source: https://habr.com/ru/post/1343908/


All Articles