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

Add static variables #118

Open
rozaxe opened this issue Nov 22, 2018 · 13 comments
Open

Add static variables #118

rozaxe opened this issue Nov 22, 2018 · 13 comments

Comments

@rozaxe
Copy link
Collaborator

rozaxe commented Nov 22, 2018

⭐️ 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 :

🐇💾🍇

	🐇🖍🆕 instance 🍬💾

	🔐🆕🍇🍉

	🐇❗️🐾 ➡️ 💾 🍇
		↪️🤷‍♂️ 🙌 instance 🍇
			🆕💾🆕❗️ ➡️ 🐇🖍instance
		🍉

		↩️ 🍺instance
	🍉
🍉

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 💾

@thbwd
Copy link
Member

thbwd commented Nov 22, 2018

Perfect except that I don't think ➡️🐇🖍instance is good. ➡️🖍instance like with instance methods would be better. After all, type methods really are in the context of the type. (🐕 contains the type.)

@thbwd
Copy link
Member

thbwd commented Nov 22, 2018

You wanna try to implement this? 🙂

@rozaxe
Copy link
Collaborator Author

rozaxe commented Nov 22, 2018

At least I will try ! 😁

@rozaxe
Copy link
Collaborator Author

rozaxe commented Nov 25, 2018

I am digging into implementing this, and I found a problem. How a static variable initial value would be set ?

🐇 🐟 🍇
  🐇🖍🆕 count 🔡

  🐇 ❗️ 🙇‍♀️ 🍇
    count ➕ 1 ➡️ 🖍 count
  🍉

  🐇 ❗️ 💁‍♂️ 🍇
    😀 count ❗️
  🍉
🍉

For example, what would be count initial value ?

Do you think we could add "on step declaration and assignment variable" in type as well ?

Something like that :

🐇 👩‍💼 🍇
  5300 ➡️ 🖍🆕 money
  🆕 🍇🍉
🍉

After calling 🆕👩‍💼🆕 the value of money would be set to 5300 ?

Thus, we could write something like that for static variables :

🐇 🐟 🍇
  0 ➡️ 🐇🖍🆕 count
🍉

@thbwd
Copy link
Member

thbwd commented Nov 25, 2018

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.

@thbwd
Copy link
Member

thbwd commented Nov 27, 2018

@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.

@rozaxe
Copy link
Collaborator Author

rozaxe commented Nov 28, 2018

No problem, go again ! (I need some time to understand how the compile works in whole, so do not hesitate to implements features)

@thbwd thbwd added this to the 1.0 milestone Nov 28, 2018
@thbwd
Copy link
Member

thbwd commented Nov 28, 2018

So we have a syntax issue here. Consider this code:

🐇 👩‍💼 🍇
  🆕🦍🆕❗️ ➡️ 🖍🆕 pet
🍉

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:

🐇 👩‍💼 🍇
 🖍🆕 pet ⬅️ 🆕🦍🆕 ❗️
🍉

@rozaxe
Copy link
Collaborator Author

rozaxe commented Nov 28, 2018

So for recap, for instance variable :

🖍🆕 pet ⬅️ 🆕🦍🆕 ❗️

And for "in method variable" :

🆕🦍🆕 ❗️➡️ 🖍🆕 pet

@thbwd
Copy link
Member

thbwd commented Nov 28, 2018

Yes, although I think it might even be

🖍🆕 pet 🦍 ⬅️ 🆕🦍🆕 ❗️

because type inference could be difficult here. Furthermore, a well-reasoned argument against something like this was made in #83 .

@thbwd
Copy link
Member

thbwd commented Nov 30, 2018

✅ This is implemented on 0.7.

https://0-7.emojicode.org/docs/reference/classes-valuetypes.html#instance-variables

@rozaxe
Copy link
Collaborator Author

rozaxe commented Dec 15, 2018

Sorry, I do not feel I will be able to implement this (too many knowledge).
I will keep my motivation for implementing some useful package !

@thbwd
Copy link
Member

thbwd commented Dec 16, 2018

@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.

@thbwd thbwd removed this from the 1.0 milestone Dec 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants