-
Notifications
You must be signed in to change notification settings - Fork 159
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
Add static variables #118
Comments
Perfect except that I don't think |
You wanna try to implement this? 🙂 |
At least I will try ! 😁 |
I am digging into implementing this, and I found a problem. How a static variable initial value would be set ?
For example, what would be Do you think we could add "on step declaration and assignment variable" in type as well ? Something like that :
After calling Thus, we could write something like that for static variables :
|
Declaring and Assigning in one step in types would be great. These expressions would have to be evaluated in a context where 🐕 (and thus access to instance variables) is disallowed, I guess. |
@rozaxe Can you please give me a heads-up before you implement anything concerning the declaration and assignment in type bodies? I want to some refactoring here first, if possible. |
No problem, go again ! (I need some time to understand how the compile works in whole, so do not hesitate to implements features) |
So we have a syntax issue here. Consider this code:
While this is a variable assignment, it is hard for the compiler to tell it apart from an initializer definition:
Emojicode uses a strict LL(1) grammar, which makes this impossible. I think the best solution is:
|
So for recap, for instance variable :
And for "in method variable" :
|
Yes, although I think it might even be
because type inference could be difficult here. Furthermore, a well-reasoned argument against something like this was made in #83 . |
✅ This is implemented on 0.7. https://0-7.emojicode.org/docs/reference/classes-valuetypes.html#instance-variables |
Sorry, I do not feel I will be able to implement this (too many knowledge). |
@rozaxe That’s alright. I myself have refrained from implementing exactly this in the past because it is rather complicated. I’ll remove it from the 1.0 milestone then, though. |
⭐️ Proposed change
I propose a way to define instance variables which can be called on the type rather on an instance of the type.
🤔 Rationale
This would allows to store values on a type, and use them from type methods.
This would also allows the development of the
singleton
pattern.🕺Example
Here an example of an implementation of the
singleton
pattern :This defines a static variable
🐇🖍🆕 instance 🍬💾
accessibles from the type itself with🐇instance
.We can then call the singleton with
🐾🐇💾❗️
and access the unique instance of the type💾
The text was updated successfully, but these errors were encountered: