Timeline for Why does the type go after the variable name in modern programming languages?
Current License: CC BY-SA 3.0
9 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Nov 27, 2023 at 15:16 | comment | added | Bill | In Kotlin, there are two keywords that are used, var and val. val implies "final" or "const" for that variable, and is preferred when it is known that value won't change. This allows the compiler to catch many common errors. | |
| Mar 15, 2022 at 10:32 | comment | added | gnasher729 | You'd just write var userInput = getUserInput(), never mentioning that it is a String at all. In Swift, what you think is a String often is not, but just an instance of some class supporting StringProtocol, and forcing it to be a String is possible, but would be quite inefficient. | |
| Jan 12, 2022 at 23:37 | comment | added | Turksarama |
@kdb the main advantage of using the var keyword in such a language is that you have to fix significantly less code if you change what type a function returns, as long as the rest of the code is compatible with the new type. Consider changing a returned float to a double, in most cases you wouldn't need to change anything except the declaration for the variable you're returning the value to.
|
|
| Apr 24, 2019 at 2:20 | comment | added | Wes Toleman |
@kdb in languages like C# and C++ it would be var userInput = getUserInput() or auto userInput = getUserInput(), the compiler knows getUserInput() returns String so you don't have to specify it with the type deduction keyword. userInput = getUserInput() is of course use before declare.
|
|
| Dec 6, 2018 at 13:47 | comment | added | kdb |
Doesn't the existence of the var keyword defeat the purpose of the name-first notation? I don't see the advantage of writing var userInput: String = getUserInput() over String userInput = getUserInput(). The advantage would only be relevant if userInput = getUserInput() is also allowed, but that would imply implicit declaraton of a scoped variable, which I find pretty abhorrent.
|
|
| Apr 20, 2016 at 13:56 | comment | added | 8bittree |
+1 for acknowledging the existence of mandatory keywords like var that provide the majority of the parsing simplification.
|
|
| Apr 19, 2016 at 19:26 | vote | accept | André Polykanine | ||
| Apr 19, 2016 at 19:00 | history | edited | Ixrec | CC BY-SA 3.0 |
added 213 characters in body
|
| Apr 19, 2016 at 18:53 | history | answered | Ixrec | CC BY-SA 3.0 |