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

Static variables in methods are static to all class instances? #91

Closed
catb0t opened this issue Mar 12, 2019 · 1 comment
Closed

Static variables in methods are static to all class instances? #91

catb0t opened this issue Mar 12, 2019 · 1 comment

Comments

@catb0t
Copy link
Contributor

catb0t commented Mar 12, 2019

PHP has this as intended behaviour, I was just wondering if this is also intentional for Sidef (I don't use it anyway, but I can see why it might be useful).

class StaticVar {
  method get_next {
    static var i = 0
    i++
  }
}

var a = StaticVar()
var b = StaticVar()

say a.get_next # => 0
say a.get_next # => 1
say a.get_next # => 2

say b.get_next # => 3
say b.get_next # => 4
say b.get_next # => 5

If it's intentional, do you think you will keep it in the future class-rewrite?

@trizen
Copy link
Owner

trizen commented Mar 19, 2019

Yes, this is intentional in Sidef too.

If it's intentional, do you think you will keep it in the future class-rewrite?

Most likely.

For separate counters on different object, we have:

class Example {
    has i = 0
    method get_next {
        i++
    }
}

@trizen trizen closed this as completed Mar 19, 2019
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