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

[Feature] Add the static global const to Leo #485

Closed
myoussefmir opened this issue Dec 8, 2020 · 8 comments · Fixed by #705
Closed

[Feature] Add the static global const to Leo #485

myoussefmir opened this issue Dec 8, 2020 · 8 comments · Fixed by #705
Assignees
Labels
documentation Improvements or additions to documentation feature A new feature.

Comments

@myoussefmir
Copy link

🚀 Feature

The C equivalent of:
#define CNAME value
#define CNAME (expression)

A syntax that creates constants that represent numbers, strings or expressions would be very useful. In its most useful form this would allow the global definition of a constant as in C equivalent: #define CNAME value.

Implementation

Are you willing to open a pull request? (See CONTRIBUTING)

@myoussefmir myoussefmir added feature A new feature. P1 labels Dec 8, 2020
@myoussefmir myoussefmir added this to the Leo Developer Preview II milestone Dec 9, 2020
@collinc97 collinc97 removed the P1 label Feb 11, 2021
@gluax gluax self-assigned this Feb 22, 2021
@gluax
Copy link
Contributor

gluax commented Feb 22, 2021

As discussed with @collinc97 #define isn't the best for this use case. Might make people think its more of a macro a la c style, rather than just a constant. As such the syntax, for now as it can be changed at a later date, would be something akin to const THING: u32 = 8; similar to the rust style.

@acoglio
Copy link
Collaborator

acoglio commented Feb 23, 2021

This could tie in with having global variables, if they are something we eventually want to have anyways. I.e. variables at the top level, not inside a function. Currently we have what, I believe, works a bit like a global variable, namely input although it is perhaps read-only.

@gluax's proposed approach sounds good. Since we recently removed (or will soon remove) const variable definitions inside functions (with const now being only used as modifier of function inputs), perhaps this example could be

let THING: u32 = 8;

for uniformity with variable definitions inside functions. We could disallow mut in global variables, at least initially, making them read-only.

@collinc97
Copy link
Collaborator

This could tie in with having global variables, if they are something we eventually want to have anyways. I.e. variables at the top level, not inside a function. Currently we have what, I believe, works a bit like a global variable, namely input although it is perhaps read-only.

@gluax's proposed approach sounds good. Since we recently removed (or will soon remove) const variable definitions inside functions (with const now being only used as modifier of function inputs), perhaps this example could be

let THING: u32 = 8;

for uniformity with variable definitions inside functions. We could disallow mut in global variables, at least initially, making them read-only.

Yes, the intention is to support global variables. Adding input as a default/reserved global variable is still tbd and waiting on finalizing the programming model.

Personally, I feel okay with allowing the const keyword back into the language for this feature. However, I do think it may conflict with the const function parameter feature that was just added so maybe let statements are best.

I am also on the fence about the ALL UPPERCASE naming of the constants. In rust a warning is emitted when a const variable is declared without uppercase. However, this always felt a little obnoxious to me and see no problem allowing normal identifiers by default

@gluax
Copy link
Contributor

gluax commented Feb 23, 2021

Adding back to this discussion. @collinc97 right now I have the syntax using const but I believe the way I implemented the feature it does not conflict with the const function parameters at all.

I currently have it as allowing both lowercase and uppercase syntax, with no warning being generated on lowercase. I would say its better to keep the uppercase for constants warning to a linter or etc as that's what most languages do(rust is an exception I believe).

Furthermore I would like to bring up another point of discussion. If importing another leo file, do we import the global constants? I know some languages do this by default, but couldn't remember if rust does or not. In either case what direction would we like leo to take?

@damirka
Copy link
Contributor

damirka commented Feb 23, 2021

Small question to the topic.

Are we considering Circuit-level static constants? Say, you could have Math circuit with U32_MAX constant in it. Which seems (at least to me) logical and handy when importing and using external circuit. Or do we already have a decision on it?

@gluax
Copy link
Contributor

gluax commented Feb 23, 2021

Small question to the topic.

Are we considering Circuit-level static constants? Say, you could have Math circuit with U32_MAX constant in it. Which seems (at least to me) logical and handy when importing and using external circuit. Or do we already have a decision on it?

@damirka this issue only seems to be adding top level constants for now. I don't think we have a discussion on static const circuit fields, but we can definitely open another feature request for one. I think it would be a good addition to the language.

@gluax gluax added the documentation Improvements or additions to documentation label Feb 23, 2021
@acoglio
Copy link
Collaborator

acoglio commented Feb 23, 2021

@damirka I think that what you propose would be a natural and useful extension of Leo. (Just my vote.)

@collinc97
Copy link
Collaborator

Small question to the topic.

Are we considering Circuit-level static constants? Say, you could have Math circuit with U32_MAX constant in it. Which seems (at least to me) logical and handy when importing and using external circuit. Or do we already have a decision on it?

Added a feature request here!

@collinc97 collinc97 changed the title [Feature] Add the equivalent of global #DEFINE to Leo [Feature] Add the static global const to Leo Mar 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation feature A new feature.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants