Timeline for Why do you have to specify the data type when declaring variables?
Current License: CC BY-SA 3.0
11 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Mar 21, 2016 at 18:53 | comment | added | user20416 | This answer isn't clear enough about the distinction between type inference, where the variable has a type (e.g. var in C#); and dynamic typing, where no type is inferred from the value because the variable has no type. | |
| Mar 21, 2016 at 16:32 | comment | added | user20416 | @MSalters The type of x .... How is this the type of x and not the type of the value to which x is pointing? Why not just say that x has no (inherent) type whatsoever? | |
| Mar 21, 2016 at 16:29 | comment | added | MSalters | @ZevSpitz: Indeed. The type of x dynamically changes from number to string to date. You could even have x = (rand() ==0) ? "string" : 5 and then the type of x will be random (!) | |
| Mar 21, 2016 at 16:26 | comment | added | user20416 | @MSalters Your words: The definition of a dynamically typed language is that types attach to values, not variables so I can reassign a value of any type to any variable, regardless of the initially assigned type -- var x=5; x=""; x= new Date();. | |
| Mar 21, 2016 at 12:40 | comment | added | MSalters | @ZevSpitz: The first kind of system isn't dynamically typed, but not typed at all. Your Javascript example isn't dynamically typed, precisely because the Number type cannot change. In a dynamically typed language, x = ""; changes the type of x to string, even if it was a number previously. | |
| Mar 21, 2016 at 9:35 | comment | added | user20416 | @MSalters Assigning a value to a variable therefore sets the variables's type as well. Or that the variable has no inherent type and the interpreter will attempt to apply whatever operation to the value of the variable. Are there any dynamically typed languages where code like the following (Javascript) would not be allowed var x = 5; x = ""; because the first statement causes x to have the "Number" type associated with x? Sort of conflicts with dynamic typing. And if not, what effect does the type associated with the variable have, beyond the type association with the value? | |
| Mar 21, 2016 at 1:34 | comment | added | Derek Elkins left SE | The irony here is this includes C# with this exact syntax. | |
| Mar 20, 2016 at 23:35 | comment | added | Robert Harvey | @MSalters: I made a slight adjustment to the wording. | |
| Mar 20, 2016 at 23:35 | history | edited | Robert Harvey | CC BY-SA 3.0 | added 15 characters in body |
| Mar 20, 2016 at 23:31 | comment | added | MSalters | The var name = "Ali" style is actually common for modern statically typed languages. In statically typed languages, the type is fixed at creation, but it still can be determined by the initializer. The definition of a dynamically typed language is that types attach to values, not variables. Assigning a value to a variable therefore sets the variables's type as well. | |
| Mar 20, 2016 at 23:23 | history | answered | Robert Harvey | CC BY-SA 3.0 |