18
votes
javascript closure = c# properties?
You are mixing up different things here, probably because you have seen examples how closures can be used to simulate the concept of public and private fields in a language which does not have these ...
13
votes
Accepted
Is this extensive usage of closure a known (anti-)pattern? And does it have a name?
This is just using closures.
When you're from an OO background building and using a closure ends up feeling like using constructors/factories and methods, respectively. The big difference is you just ...
9
votes
javascript closure = c# properties?
C# also has closures. An example:
class A {
Func<int, int> CreateClosure() {
int a = 17;
return x => x + a;
}
}
The method returns a closure (the function x => x + a), which ...
7
votes
Accepted
Why closure declaration syntax in Swift is different from function declaration
Swift chose lambda-style syntax for their function parameters in ordinary functions as well. The only difference between an ordinary function and a closed-over one is the positioning of the braces ...
7
votes
Accepted
What is the use case for shadowing variables?
The issue with the way you describe it (I don't know coffeescript myself) is that it makes it too easy to break existing code without realizing it. Let's say you have this code:
bar = 5
// 100 lines ...
5
votes
Are a class's methods a type of closure?
Yes, but global variables have nothing to do with it.
A closure is a function that encloses state from an outer scope. This enclosed state is persistent, and more than one function can enclose the ...
5
votes
Accepted
What is the most generic way to provide a variable amount of outputs from a Rust function?
I don't think that the use of iterators you propose is an abuse of iterators at all. It's perfectly acceptable for iterators to perform computation and for them to have significant amounts of state. ...
4
votes
Accepted
What is the difference between explicit parameters and closures
From the compiler's point of view, there's very little difference between a closed over variable and a function argument. They are just given to the function's symbol table at different times.
In ...
4
votes
Understanding closures
It's not about an outsider being able to change it. Here is a better example which illustrates the usefulnes of closures:
var getCodeForUser = (function(username){
var secureCode = CalculateCodeFor(...
3
votes
Accepted
Is there a name for a closure which just returns a value?
In short
No, there is no special name
Some more explanations
Since you capture the variable by value and not by reference, and since PHP uses early binding, this closure will indeed return the value ...
3
votes
javascript closure = c# properties?
I really wish blog writers would stop promulgating the privacy aspect of closures. It's a weak usage of them and barely worth mentioning. What's worth knowing about them is the communication aspect, ...
3
votes
Is this extensive usage of closure a known (anti-)pattern? And does it have a name?
As @candied_orange mentioned, what you're doing there is very similar to currying. Its worth learning about.
Javascript arrow functions were designed to allow for easy currying. An example of a fully ...
3
votes
Using a closure to avoid code duplication in Python
I am not a fan of OP's style here. The inner function is not testable and not reusable. In addition, if either the inner or outer function grows significantly long / complex, it becomes a mess to read ...
2
votes
Accepted
Are a class's methods a type of closure?
Are a class's methods a type of closure?
No. A closure is a very specific type of function. As Wikipedia says, they implement lexical scoping.
Closures
In Python, such a closure would look like ...
2
votes
A JavaScript IIFE prevents leaks to the global space as a closure? Is that the correct concept?
I think it is a local scope, or an anonymous local scope, that is shield any local variables from leaking to the global scope.
Yes, that's precisely what it is. And because JavaScript doesn't ...
2
votes
What is the difference between explicit parameters and closures
Afaic, at least in C#, at least sometimes behind the scenes a class is generated to store captured variables in it's fields. It is a significant overhead compared to stack allocation to pass parameter ...
2
votes
javascript closure = c# properties?
No. If you want to compare to C# a better analogy is constructor parameters.
Lets say an api requires you to supply it with a callback with no arguments.
In OO you would typically construct an ...
2
votes
Is this extensive usage of closure a known (anti-)pattern? And does it have a name?
Functions are objects. The have but one entry point instead of multiple methods but this limitation does not change their basic nature. Closures are configurable objects. When closures capture ...
2
votes
Are nested closures a good thing?
The ability to have nested functions is a good thing, but obviously any code that is confusing is not a good thing. This kind of code is ubiquitous in the context of asynchronous programming of which ...
2
votes
Accepted
Is it possible to have nested functions, without having closures, in a lexically scoped language which has first class functions?
I don't think your conclusion is true.
function outer1() {
const foo = 42;
function inner1() {};
}
In this case, inner1 is not a closure, but it is a nested function. It simply doesn't use any ...
2
votes
Accepted
Purpose of Closure Style in Laravel/PHP?
The Laravel route directives actually do accept a string as the second parameter:
Route::get('/user', 'UserController@index');
This is parsed by the Laravel router to call the desired action in the ...
1
vote
Are nested closures a good thing?
You can try something like this:
func signIn(email: String, password: String) -> User? {
var user: User? = nil
DispatchQueue.global().async {
let semaphore = DispatchSemaphore(...
1
vote
Accepted
Understanding closures
In your particular contrived example, there is no difference at all. If you only ever use a variable by value, and assign it a compile-time value, it matters not whether you use a closure or not. In ...
1
vote
Understanding closures
People often get excited talking about what closures can do, and they forget to talk about what you should do with closures. Your particular example builds on the previous example, which showed ...
1
vote
Accepted
Javascript Closures to Separate Class
It sounds like you've discovered that you can use javascript closures to create classes. Doing this you can treat javascript as an object oriented language. But is this wise?
Look at java 8. Java ...
1
vote
What is the use case for shadowing variables?
The backlash towards CoffeeScript specifically is likely because CoffeeScript is supposed to be a language that is similar to Javascript; it is advertised as a variation or extension of Javascript. ...
1
vote
Is a lambda expression something more than an anonymous inner class with a single method?
Joachim Sauer already did a good job answering you question but only hinted at something I regard as important. Since Lambdas are no classes they are also not compiled as such. All anonymous inner ...
1
vote
Are a class's methods a type of closure?
I think what you're asking is whether static methods and static variables of the same class can be considered as a (perhaps poor person's) closure construct.
While it has some resemblance to the ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
closures × 49javascript × 19
java × 3
c# × 3
terminology × 3
functional-programming × 3
language-features × 3
swift-language × 3
lambda × 3
design-patterns × 2
object-oriented × 2
c++ × 2
php × 2
python × 2
programming-practices × 2
programming-languages × 2
language-design × 2
scala × 2
scope × 2
design × 1
web-development × 1
coding-style × 1
interfaces × 1
naming × 1
language-agnostic × 1