What does this thing do?
On hover it says “invalid attribute”
I haven’t found anything about it (and I lowk don’t trust Gemini)
Is this a half-done feature?
This might help: What does the @ symbol do - #4 by Dekkonot
You can use it one line above a function
@deprecated was added as well since the post
They also refuse to allow you to create custom attributes/decorators, though, which is annoying. Currently, the only workaround I can think of is
local myFunc = decorator1(decorator2(function(...) ... end))
which is annoying, but fine. I do not understand why they don’t let you define your own.
yeah, I know. I’ve read the RFC. I still don’t agree with it. I probably should have phrased what I said better, instead of “do not understand why” it would’ve made more sense to say “do not agree with why”
Now, I do see that they said this
but that honestly just makes me more confused. AFAICT these are just compiler defined decorators, no need for a different word…whatever, I’m fine with local foo = decorator(function(...) ... end)
lol
Not really, for example the @deprecated
attribute doesn’t change any code or logic, it simply marks the function as deprecated so the linter can raise a flag whenever it sees it being used. The @native
flag also just tells the compiler to use native codegen for the following function, still doesn’t change code logic or the function itself (debatable). “Marking” functions would be the best way to explain it; it’s not supposed to be decorators like in python that wraps around the function with padding code. But I understand, it can get very confusing especially since that they are introducing attributes with parameters now*, which shares a lot of similarities with actual decorators.
*They did for a few hours until it was discovered the feature is still incomplete.
if they wanted “attributes” like this then why did they use a decorator syntax
that’s genuinely such a bad decision
From what I can tell it’s the least bad option out of all other alternatives that either have conflicting syntaxes or violates some kind of convention or sets a really bad precedent for the language.
- Tilde
~
is used in logic inequality - Exclamation
!
is used by comment directives - Backtick ` is used in string interpolation
- Hash
#
is used for the length operator - Dollar sign
$
by convention has more to do with variables - Percent
%
is used by modulo operator - Carat
^
is used by exponent operator - Ampersand
&
is used by type intersection - Asterisk
*
is used by multiplication - Parentheses, brackets, and braces of any kind will cause ambiguous syntax
- Vertical bar
|
is used by type union - Double slash
//
is used for floor division (funny enough, this was originally a rejected RFC because it conflicted with C’s comment syntax) - Backslash is used for ASCII and string escape characters
The at symbol @
is the only thing not in use that is even remotely related to attributes, because it shares the most in common with decorators in that it has something to do with functions.
Yeah, that’s basically my point. They could just call it a compiler-defined decorator that doesn’t directly modify the function, rather does stuff in the compiler and editor.
Still calling it a decorator is still not a good idea lol, if you want to emphasize to people that it’s not a decorator then you would want to make up a completely different name and don’t even mention decorators at all to not give them the slightest idea of it being what it’s not
yeah I know, I completely get the reasoning behind not wanting to call it a decorator, I’m just saying that it makes no sense to me from the perspective of not wanting luau to just be simple
Hi, I’m the author of the RFC.
It was because it had to be some kind of symbol and it had to be obviously attached to the function. @deprecated
and @native
being before a function felt obvious.
As to why attributes over decorators, it’s a complexity thing. User defined attributes could happen, but an RFC for them is really hard to write without the syntax being decided first.
Yeah, I’ve basically figured the reasoning out by now. I really don’t know why I was acting as I was before, @
does makes the most sense (i do stand by “compiler-defined decorator” being a better name, though!) Thanks, though.
Can’t something like Python’s decorators be done? Been a bit since I wrote Python, but I believe it’s done like this:
def my_decorator(f):
def wrapper():
print("Before execution")
result = f()
print("After execution")
return result
return wrapper
@my_decorator
def foo():
...
so in Luau that could be this:
function myAttribute(f)
return function()
print("Before execution")
local result = f()
print("After execution")
return result
end
end
@myAttribute
function foo()
...
end