Supporting multiple initializations on the same line, x,y,z: float = 1,2,3; #1139
Replies: 6 comments 8 replies
-
Personally, I do not declare multiple variables on the same line. I find it quite hard to read. This is also because pointer and reference declarations in C++ are right binding so Nevertheless, I can understand why this kind of syntax has its benefits. But for your proposed syntax the readability is not very great. Especially for a larger number of variables, the reader of the code has to count out the commas in or to know which variable is assigned to which value. Do you have any suggestion on how to address this issue? If |
Beta Was this translation helpful? Give feedback.
-
I am contemplating allowing multiple names in the future but have not implemented it, in part because I haven't yet decided whether I want to support only destructuring or also multiple actual objects (in which case destructuring would require parens or similar). I think the motivation for a destructuring syntax is strong, and the motivation for multiple variables of the same type feels much weaker:
So for now I don't want to support multiple declarations in the same statement, other than structured bindings which are on my todo list. And if I rule out multiple declarations in the same statement, then I can use that no-paren syntax at top for structured bindings. |
Beta Was this translation helpful? Give feedback.
-
I think that while structured bindings are useful, perhaps now is a good time to try to figure out a better solution? Structured bindings require auto type deduction, removing the ability to specify the type of a variable in a strongly typed language could be seen as a failure of the current language feature.
Im not saying I have a better solution, but simply that we have a chance for some out of the box thinking here
On 29 June 2024 02:39:57 Herb Sutter ***@***.***> wrote:
Quick ack: Those are very reasonable points, thank you. In particular that any bindings would already allow multiple names on the lhs of declarations, for objects only.
—
Reply to this email directly, view it on GitHub<#1139 (reply in thread)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AALUZQODR4POKQLVEWSW3WLZJYF6VAVCNFSM6AAAAABJYYNZE6VHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TSMBZG44DQ>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
We could retain the ability to strongly type the types in structured bindings if we allowed multiple same-line declarations for objects. I think it boils down to 3 choices: Multiple comma-separated object identifiers allowed: 1. Only on the left of the
2. On the left and right of the
3. Neither on the left and right of the In any of the cases 1 and 2, we should be able to explicitly declare the types next to |
Beta Was this translation helpful? Give feedback.
-
Whatever the syntax is, it needs to work in for loops too, which use a : character, making typing structured bindings in a for loop awkward
On 29 June 2024 15:27:01 HALL9kv0 ***@***.***> wrote:
removing the ability to specify the type of a variable in a strongly typed language could be seen as a failure of the current language feature.
We could retain the ability to strongly type the types in structured bindings if we allowed multiple same-line declarations for objects.
I think it boils down to 3 choices: Multiple comma-separated object identifiers allowed:
1. Only on the left of the :=
a, b := stuctBindingA(); \\ (A) structured binding
c, d := 0; \\ (B) multiple initializations with the same value
e, f: int; \\ (C) just declarations
g, h: int, int; \\ (D) just declarations
2. On the left and right of the :=
a, b := 1, 2; \\ (A) multiple-inits
a, b, c, d := stuctBindingA(), stuctBindingA(); \\ (B) multiple structured bindings - Pretty much GO
3. Neither on the left and right of the :=
The current status quo
In any of the cases 1 and 2, we should be able to explicitly declare the types next to : .
Based on my personal preference I would say that we need at lease cases 1.
Case 2(A) would be a nice convenience for certain situations, but I'm not a fun at all of case 2(B).
I think if case 2(A) is allowed, case 2(B) will eventually come up as a request.
—
Reply to this email directly, view it on GitHub<#1139 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AALUZQNCSEJW4SI5QPCLOQTZJ273FAVCNFSM6AAAAABJYYNZE6VHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TSMJSGM3TK>.
You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
This commit adds an explicit error for multiple inits on the same line. 8504e86 |
Beta Was this translation helpful? Give feedback.
-
I was playing around with cpp2 ans SFML and I had to initialize many variables of the same type, with different values. For example x,y,z speed, angle etc. So I ended up having 7+ lines of variable initializations with a repetition of the characters
: float =
.I thought it would be much more concise and cleaner to have a single line of code like
x,y,z,angle: float = 1,2,3,4;
, similar to Go. It would be closer to how you would describe it: " I have variables x,y,z, angle of type float, which I initialize to values 1, 2,3,4". By extension this could be used in:=
and:const=
.I would like to know if others also think this is a nice feature to have. Sorry, if this has been suggested before, I searched for it but i didn't find anything.
Beta Was this translation helpful? Give feedback.
All reactions