-
Hi! I always thought that we use "auto" in (standard) c++ in the following piece of code: auto i = 3; just because doing just i = 3; would be "too disruptive". But now I see that even when we are free from backward compatibility, there it is "auto" again, this time with a new symbol "_". I was just curious, why can't we get always away without specifying the type at all when we want it to be automatically inferred, on in other words, why can't we denote "auto" as ""? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 17 replies
-
That's a good question. Actually fnc1: (x: _) -> _ = x + 1;
fnc2: (x) x + 1;
var1: _ = 10;
var2: = 10;
// Type template parameter
cls1: <T: type> type = { }
cls2: <T> type = { }
// Non-type template parameter
cls3: <T: _> type = { } Because "type template parameters" are the default type. If we want "non-type template parameters", we have to explicitly write it. |
Beta Was this translation helpful? Give feedback.
-
I think it would become a cause of bugs. Imagine in a new scope you want a variable with a common name like i, so you use i = 0, and it actually sets the value in an outer scope. At least with i: = 0 the cpp2 transpiler can warn about a variable name being reused, and the person maintaining the code can see that a new variable was intended, rather than have to guess that's what the author of the code meant
On 12 October 2023 09:04:17 Marco Correia ***@***.***> wrote:
Oh I see, it would be impossible to distinguish those two cases if we don't include the type specifier prefix ":". Thanks.
—
Reply to this email directly, view it on GitHub<#747 (reply in thread)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AALUZQPYSQQF5YBEMSNGWSDX66P75ANCNFSM6AAAAAA55FJV44>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Actually
fnc3: (x) x + 1;
|
Beta Was this translation helpful? Give feedback.
That's a good question. Actually
_
is optional except for template parameters:Because "type template parameters" are the default type. If we want "non-type template parameters", we have to explicitly write it.