2

I have a PHP closure defined as function () use ($contents) { return $contents; }. It's like a value object except it's a closure. Is there a name for this?

8
  • 1
    This question's gotten -3 votes pretty quickly, though it may not be clear why. Off-topic, poor research, unclear, etc.? Commented Jul 31, 2020 at 9:25
  • 2
    Being a closure is about having access to the immutable state of an enclosing scope. It's a lot like instance state for methods. Returning a value has nothing to do with that. Now a pure function, closure or not, must return a value or it's pretty useless since it's not allowed to do anything else. Commented Jul 31, 2020 at 10:46
  • @nat, it's mostly because there isn't a name. The only way to avoid question downvotes on this site is to already know the answer. Commented Jul 31, 2020 at 14:07
  • 1
    @KarlBielefeldt: That's a bit cynical, isn't it? Here's me being cynical: not everything has a name. Not everything has to have a name. The folks who insist on naming everything are the ones with so little creativity that everything must be "correct" or they can't cope. Commented Jul 31, 2020 at 16:29
  • 1
    I'm really not clear on which aspect of this anonymous function you're trying to name and what other names you've rejected and why. Commented Jul 31, 2020 at 23:58

2 Answers 2

3

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 of $contents at the time the closure was created. You could as well just return a constant.

But this does not matter. All the interest of a closure is to define a behavior on the fly, that can be executed as a normal function, if needed with arguments. Whether the value is constant or a very complex calculation only matters to the context that defined the closure. The context that uses the closure doesn’t know what the closure will return.

There is hence no reason to give a name to this particular form of closures.

Would you look for a name anyway:

  • a closure defines an unnamed function. So for categorizing closures according to specific behavior the same terminology should bed used as for normal functions.
  • a function that returns a constant value is called a constant function in mathematics. So a constant closure (or constant lambda expression in other languages) could be candidate.
  • unfortunately, in PHP the constant function is already taken, so that this might create some ambiguities.
  • furthermore it’s not the closure itself that is constant, but the value it returns.

All this makes a closure returning a constant the best candidate. Of course this is not very original, and it’s certainly not among the most frequent use of closures in the real world. Maybe this is why nobody really bothered to find a name for it ;-)

1
-1

"Expression".

An object primarily intended to represent a value is often called an "expression".

Usually expressions are argument-less, though they may have embedded arguments. For example, 1+2 can be called without arguments.

"Expression Trees (C#)", Microsoft Docs, shows some of the basic logic in the context of C#.


Constant-expressions vs. literal-expressions.

If you have a simple literal value in the source-code, then that's a "literal expression". The defining quality of a literal expression is that the conventions of the environment don't require it to be evaluated; it's considered understood intrinsically.

If you have something like an enclosure that needs to be evaluated to return the same value, then that's a "constant expression".

In short, if you're just referring to a simple value with no additional calculations being done, then it's a "constant expression" if an evaluation still needs to be done (as with a closure) while it's a "literal expression" if no evaluation needs to be done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.