As stated by on Re-evaluate core variable & parameter identifier/type order (including a default for parameters) #542:
Somewhat condensed, bullet-point-y background for this question:
- We've been using first
Type: variableand thenType variablesyntax in variables, parameters, and other declarations.- This was primarily based on a lack of compelling data to select a better syntax, with trying to stay similar to C++ as a fallback.
- It was specifically intended to be revisited. The expected trigger for this was some form of broader user data (surveys at least of decent #s of developers, or potentially real user studies).
- However, we have gained specific new information as we've filled out a great deal of the surrounding syntax. We have also gotten some data on parsing challenges (although perhaps surmountable challenges) of
Type variable.- We also don't have a short/definite timeline to start getting useful data.
- The leads should re-evaluate the core variable syntax based on the new information we have, but without trying to wait for data.
- We can always re-evaluate again if and when data arrives and indicates any need for it.
- The leads should do this ASAP and make a decision so that we can focus our energy, reduce frustrating discussions, and have consistent syntax in examples and proposals.
Background may be found in the related #542 and Docs.
Two changes:
<name>: <type>, replacing <type>: <name>.in instead of : in range-based for loops.Note these changes were largely implemented by #563.
Both of these changes are done for consistency with other modern languages,
particularly Swift and Rust. The switch from : to in is for ease of
understanding and parsing.
Alternatives are pulled from Docs.
<type>: <name>var String: message = "Hello world";
Advantages:
var
and :.Disadvantages:
: put the name before and the type after
(universally).: in this syntax with
different order will add confusion for people working/familiar with multiple
languages.Opinions vary:
type: to represent auto type
deduction.<type> <name>var String message = "Hello world";
Advantages
Disadvantages:
Currently hard to see how we can make this work, since it isn't compatible with other choices, detailed in Docs.
<name>: <type>var message: String = "Hello world";
Advantages:
struct definition.Disadvantages:
Opinions vary:
: type" part optional when an
"= value" clause is present.: versus inThe : operator for range-based for loops becomes harder to read, and more
likely to cause ambiguity, when : is also used for var. That is,
for (var i: Int : list) is just harder to understand than
for (var i: Int in list). in is a favorable choice for its use in other
languages.