emojicode / emojicode

πŸ˜€πŸ˜œπŸ”‚ World’s only programming language that’s bursting with emojis

Home Page:https://emojicode.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add static variables

rozaxe opened this issue Β· comments

⭐️ 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 πŸ’Ύ

commented

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

commented

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 ?

πŸ‡ 🐟 πŸ‡
  πŸ‡πŸ–πŸ†• 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
πŸ‰
commented

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.

commented

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

commented

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 ⬅️ πŸ†•πŸ¦πŸ†• ❗️
πŸ‰

So for recap, for instance variable :

πŸ–πŸ†• pet ⬅️ πŸ†•πŸ¦πŸ†• ❗️

And for "in method variable" :

πŸ†•πŸ¦πŸ†• β—οΈβž‘οΈ πŸ–πŸ†• pet
commented

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 .

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 !

commented

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