Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document that @name is a method call if self.name is a function #436

Open
bpj opened this issue Mar 16, 2021 · 4 comments
Open

Document that @name is a method call if self.name is a function #436

bpj opened this issue Mar 16, 2021 · 4 comments

Comments

@bpj
Copy link

bpj commented Mar 16, 2021

Please consider these three expressions:

  1. @\foo bar, @baz
  2. @foo bar, @baz
  3. @.foo bar, @baz

Currently 1 and 2 both compile into

self:foo(bar, self.baz)

while 3 compiles into

self.foo(bar, self.baz)

with 2 interpreted as a method call, which I find rather confusing. I did rather expect 2 to be interpreted like 3, especially since non-calling context @baz compiles to self.baz in all cases!

I'm not sure that this is a bug (I have not found it in the documentation) but it seems inconsistent.

@daelvn
Copy link

daelvn commented Mar 16, 2021

There is a small snippet in the documentation that kind of alludes to this, but is definitely not clear enough:

The calling semantics of @@ are similar to @. Calling a @@ name will pass the class in as the first argument using Lua’s colon syntax.

I would definitely welcome (as a user) a clarification on the docs. Perhaps it could be PR'd.

Regardless, I'm pretty sure that it is expected behavior that @foo is self.foo unless @foo is a function call. @foo is generally preferred over @\foo, at least in all instances of MoonScript code I've seen. The MoonScript compiler source itself uses this syntax with the current compiled behavior. As such, I doubt this is a bug or that it will change at all.

@bpj
Copy link
Author

bpj commented Mar 16, 2021

Thanks for your input

Apart from having to decide whether to eyeball through ~600 lines of code to insert dots in sundry places or to wrap some non-method functions adopted from other libraries in methods I can live with it even though IMO it is an inconsistency (and I'll probably keep writing self-method calls as @\foo since it makes them easy to spot!) It definitely should be clearly documented though. I'll probably do a PR if I find an obvious place to put it.

@bpj bpj closed this as completed Mar 16, 2021
@bpj bpj reopened this Mar 16, 2021
@bpj bpj changed the title Calling property of self as function? Document that @name is a method call if self.name is a function Mar 16, 2021
@leafo
Copy link
Owner

leafo commented Mar 18, 2021

How it works is intentional.

  • @hello "world" is shorthand for self\hello "world"
  • @hello is shorthand for self.hello

Since both of these operations are the most common uses of self, they are made easily accessible with the @ operator directly

@bpj
Copy link
Author

bpj commented Mar 9, 2023

I realize that it is intentional, but I still think the docs should explain it explicitly. I have taken to always using the explicit @\method @.other syntax since I think it leads to fewer bugs. I may be showing my age in more than one way, but I'm more comfortable this way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants