Description
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?
foo : mut _ = 2; //corresponds to currrent syntax
foo : var _ = 2; //again corresponds to current syntax but with different keyword
var foo := 2 //corresponds to how we decorate functions with constexpr or consteval in cpp unless Herb has plans to change it in
// the future
I suggest the last alternative as a binding being mutable or immutable should NOT be part of the type,int
is type but const int
is not and so shouldn't be mutable int
or variable int
.
Next up, as constinit
and consteval
do not have overlapping use, both can be unified under one construct const
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:
CPP2 CODE GENERATED CPP CODE
a := 2; auto const a {2};
var a := 2; auto a {2};
const a := 2; auto constexpr a {2};
const var a := 2; auto constinit a {2}; //does not compile
const a : () -> int [[nodiscard]] auto consteval a () -> int;
//not sure where consteval would go
const { } consteval { }
if const { } if consteval { }
Notice how const var
spells constinit
and we don't have to teach that constinit
is just constexpr
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 cpp const__
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 the constexpr
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!