-
Notifications
You must be signed in to change notification settings - Fork 261
[SUGGESTION] Unifying various "const"s #255
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
Comments
Hi, Thank you for your suggestion. My biggest concern is that it breaks the left-to-right approach, making it context-dependent and against the goals for the cppfront experiment. (check: https://youtu.be/ELeZAKCN4tY?t=1070, and https://github.com/hsutter/cppfront/wiki/Design-note%3A-Postfix-operators). Also, Herb will appreciate sticking to the Suggestion template he creates:
|
Sorry, I made it look like I'm asking for syntax change but the motive was to make it so that one keyword does things to replace both constinit and consteval, to reduce concept count. Herb will support those sooner or later and they'd have to be written out somewhere in the code, so why not as one. |
I feel like
Tangentially it would restore left-to-rightness. |
Yes, this looks natural while also preserving left-to-rightness (idk why I didn't come up with it in the first place). |
Left-to-right in the current syntax will look more like: a := const {2};
b : var _ = const {2}; |
That'll work too, my suggestion was about
^this would be as good as the other one, though the one above looks better |
Looks like Herb has already seen that redit discussion as he replied to one of the comments 4 months ago |
I was thinking about context-free parsing... my first impression was that var x : int; Is wrong until I thought about argument passing styles: fun: (inout x : int) = {
// ...
}
inout x : int; Of course, In the last weeks, I was astonished by the context-free meaning of cppfront syntax. E.g. fun( : std::vector = (1,2,3,4) ); // unnamed vector, aka temporary variable
gun( : (x) = x + 2; ); // unnamed function, aka lambda That generates the following cpp1 code: fun( std::vector{ 1, 2, 3, 4} ); // unnamed vector, aka temporary variable
gun( [](auto const& x) { return x + 2; }); // unnamed function, aka lambda Should we extend the argument passing into local variables? Then we could have one rule for variables and argument passing. I don't know yet. I like consistency and one rule instead of two. |
I think that argument passing is the only place where the left-to-right approach is stretched. |
How about : fun : ( mut x : int ) -> double = { } // just inout spelled differently
mut a : int // consistent and conveys meaning better
As about fun( : std::vector = (1,2,3,4) ); this looks so ugly but maybe once Herb implements classes, we will have a better way to do the same. I agree with your remark about context free grammar, cpp2 focuses on left-to-rightness which is not same as context free grammar. cpp2 syntax is VERY consistent which is a good thing but sometimes it conflicts with other things (like treating const as part of the type) and maybe those decisions could be revised. and hey! @filipsajdak , your idea of unifying Edit: The reddit thread has been edited and now proposes |
Regarding the ugliness of fun( : std::vector = (1,2,3,4) ); That was my first reaction, but I liked it more when I realized that it was consistent with defining variables. v : std::vector = (1,2,3,4); // named vector
fun( : std::vector = (1,2,3,4) ); // unnamed vector, aka temporary variable
f : (x) = x + 2; // named function
gun( : (x) = x + 2; ); // unnamed function, aka lambda Regarding
and he mentioned it in https://github.com/hsutter/cppfront/wiki/Design-note:-const-objects-by-default
|
fun( : std::vector = (1,2,3,4) ); For examples of this kind, I was thinking along the lines of some static member function that does the work of a contructor but I don't wanna say anything before Herb is done with implementing classes. Just think of something like: func ( std::vector::create(1,2,3,4), other_arg ); But again, don't wanna say anything prematurely. I think of Also, I really hope Herb goes forward with I would again write a summary of what is proposed but I'm not sure if I should go with a : mut _ = 2;
// or
mut a := 2; |
I don't see the parallelism between argument passing styles and local variables to be deeper than "both have a const and a mutable case". For arguments you also have |
Reddit thread suggests Herb mentions in https://github.com/hsutter/cppfront/wiki/Design-note:-const-objects-by-default
While one could argue that |
I don't think "mutable" conveys the meaning of "the caller will pass a value to the callee, which will write a value back for the caller" better than
Using
Maybe those differences can be overcome, with a new mental model and nomenclature that unifies variable declaration and parameter declaration. But without that, I think reusing a keyword in both for concepts that are slightly different isn't a win; it just risks confusion. Now all of this is orthogonal to getting rid of |
I can't help but agree with you. BTW, C++23 is here!! |
I mentioned I thought about what @jcanizales wrote and decided to gather in one table where we use argument passing styles.
Additionally, there is some common ground for arguments and local variables. Looking from intention, how you want to use the variable:
From that perspective, it has the same meaning for local variables and function arguments, right? It gives me another thought: currently
I think the current |
I have watched a Timur Doumler talk,
So maybe argument passing can be adjusted to make them useful also for defining local variables? |
I would like to hear @hsutter 's thoughts on this unification of |
I think I need to make some changes to this. Once I come up with a better version of this, i'll reopen this or open another one. |
Uh oh!
There was an error while loading. Please reload this page.
The idea is to reduce concept count by unifying constinit and consteval, and possibly change our current view of const.
Firstly, I suggest that bindings should be constant by default. Herb has an article about it in Design notes and in that article, his answer was "Mostly yes". If mostly, why not all? He mentions three places in the article where const by default is seen, I think making users write one extra keyword in one more place won't impact the code much .There's a reddit discussion about this which I suggest looking into (https://www.reddit.com/r/cppfront/comments/xwk42s/herbs_current_view_on_const_by_default/).
Next up, if we get const by default bindings (not variables), what kind of syntax would it need?
I suggest the last alternative as a binding being mutable or immutable should NOT be part of the type,
int
is type butconst int
is not and so shouldn't bemutable int
orvariable int
.Next up, as
constinit
andconsteval
do not have overlapping use, both can be unified under one constructconst
so that use of const with a variable produces constinit in output and the same with a function produces consteval. If this is implemented, the cpp2 code will look something like this:Notice how
const var
spellsconstinit
and we don't have to teach thatconstinit
is justconstexpr
but mutable.We only have to teach that const means things done at compile time, what things?
-- for bindings, their INITIALIZATION is done at compile time.
-- for functions, their EVALUATION is done at compile time.
Issues with this proposal:
I have no idea how hard implementing something like this would be, having to know which cpp2
const
corressponds with which cppconst__
could be hard to implement in a parser.You must have noticed how I have said nothing about
constexpr
with functions and to be fair, I have no idea. One way could be just to keep theconstexpr
keyword in but not allow it for bindings (since there will be a way to declare constexpr bindings).Maybe this idea is just too raw to be implemented or could be more refined and discussed about before implementing, or maybe it's just not good enough, idk. Express your opinions please!
The text was updated successfully, but these errors were encountered: