mirror of
https://github.com/luau-lang/rfcs.git
synced 2025-04-19 03:13:49 +01:00
rfc: named function expressions
This commit is contained in:
parent
010be579d9
commit
9bb61ab2e9
1 changed files with 59 additions and 0 deletions
59
docs/named-function-expressions.md
Normal file
59
docs/named-function-expressions.md
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
# Named Function Expressions
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
This proposes that the current function expressions, which are always anonymous (unnamed) be allowed to have names.
|
||||||
|
|
||||||
|
## Motivation
|
||||||
|
|
||||||
|
This would make some code more readable, enable luau to give function expressions names other than `<anonymous>` in stack traces and with `debug.info`, enable function expressions to be recursive (call themselves), and help in miscellaneous profiling and debugging tools.
|
||||||
|
|
||||||
|
## Design
|
||||||
|
|
||||||
|
The current grammar for function expressions is the following, taken from the `simpleexp` rule.
|
||||||
|
|
||||||
|
```
|
||||||
|
attributes 'function' funcbody
|
||||||
|
```
|
||||||
|
|
||||||
|
This proposal would change that grammar to the following.
|
||||||
|
|
||||||
|
```
|
||||||
|
attributes 'function' [NAME] funcbody
|
||||||
|
```
|
||||||
|
|
||||||
|
This adds an optional name in function expressions, which can be used like so:
|
||||||
|
|
||||||
|
```luau
|
||||||
|
baz(function factorial(n)
|
||||||
|
return if n == 1 then 1 else n * factorial(n - 1)
|
||||||
|
end)
|
||||||
|
```
|
||||||
|
|
||||||
|
It is also important to note where this creates a binding to the function expression, the following example should explain:
|
||||||
|
|
||||||
|
```luau
|
||||||
|
-- 'bar' is not bound here
|
||||||
|
|
||||||
|
local foo = function bar(n)
|
||||||
|
-- 'bar' is bound here
|
||||||
|
end
|
||||||
|
|
||||||
|
-- 'bar' is not bound here
|
||||||
|
```
|
||||||
|
|
||||||
|
## Drawbacks
|
||||||
|
|
||||||
|
This proposal would make table function field syntax impossible without a special case. An example of this syntax:
|
||||||
|
|
||||||
|
```luau
|
||||||
|
local t = {
|
||||||
|
function foo() end,
|
||||||
|
}
|
||||||
|
|
||||||
|
t.foo()
|
||||||
|
```
|
||||||
|
|
||||||
|
## Alternatives
|
||||||
|
|
||||||
|
The alternative is defining a function with `local function` declaration syntax, which allows for recursion and gives debugging names.
|
Loading…
Add table
Reference in a new issue