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

Strange @__class calling behaviour #49

Closed
fillest opened this issue Nov 4, 2012 · 2 comments
Closed

Strange @__class calling behaviour #49

fillest opened this issue Nov 4, 2012 · 2 comments

Comments

@fillest
Copy link

fillest commented Nov 4, 2012

class Test
  new: (param) =>
    print param

  clone: =>
    assert @__class == Test
    @__class 'test2'
    Test 'test3'
    c = @__class
    c 'test4'

t = Test 'test1'
t\clone!

prints:

test1
table: 0x12e8c40
test3
test4

Is it a bug? I think calling @__class should work same as assigning @__class to a variable and then calling it.
Btw it translates to

      assert(self.__class == Test)
      self:__class('test2')
      Test('test3')
      local c = self.__class
      return c('test4')
@leafo
Copy link
Owner

leafo commented Nov 4, 2012

It's not a bug, whenever you call a name preceded by @, then the value of @ is passed in as the first argument.

@hello "world"

is the same as

self.hello self, "world"

So the same rules apply to @__class.

The special form @@ is a shortcut to @_class like @ is a shortcut for self, but when you call @@ nothing is passed in as the first argument. So you can do:

@@ "test2"

To make a new instance of the current class. http://moonscript.org/reference/#_and__values

@fillest
Copy link
Author

fillest commented Nov 4, 2012

Aah ok thx.
Btw seems http://moonscript.org/reference/ is being too agressively cached. I opened the link, and there was no "@@" and it appeared after ctrl+f5. Firefox, linux.

@leafo leafo closed this as completed Dec 28, 2015
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

2 participants