In computer science the term variable typically refers to a unique identifier corresponding to an allocated or reserved memory space, into which values can be stored and from which values can be retrieved. As long as the memory space is reserved, it can be assigned new values over and over again. Thus, the contents of the memory space are dynamic, or variable.
In most languages, such as C, Java, PHP, Python, and Ruby, this is the typical pattern for working with named, assignable memory storage. Variables are dynamic, mutable, and reassignable (with the exception of those defined with special restrictions such as Java’s final keyword).
In Scala, values are preferred over variables by convention, due to the stability and predictability they bring to source code. When you define a value you can be assured that it will retain the same value regardless of any other code that may access it. Reading and debugging code is easier when a value assigned at the beginning of a code segment is unchanged through the end of the code segment.
Finally, when working with data that may be available for the life span of an application, or accessible from concurrent or multithreaded code, an immutable value will be more stable and less prone to errors than mutable data that may be modified at unexpected times.
However, in those places where variables are more suitable, such as local variables that store temporary data or accumulate values in loops, variables will certainly be used. Now that the preference for values over variables has been explained in detail, we can put that aside and cover how to use variables in Scala.
The var keyword is used to define a variable with a given name, type, and assignment.
Syntax: Defining a Variable
Like values, variables can be defined with or without an explicit type. If no type is specified the Scala compiler will use type inference to determine the correct type to assign to your variable. Unlike values, variables can be reassigned new data at any time.
Here is an example of defining a variable and then reassigning it, in this case to the product of itself and another number:
Although a variable can be reassigned, its designated type cannot, and so a variable cannot be reassigned data that has an incompatible type. For example, defining a variable of type Int and then assigning it a String value will result in a compiler error:
However, defining a variable of type Double and assigning it an Int value will work because Int numbers can be converted to Double numbers automatically:
In most languages, such as C, Java, PHP, Python, and Ruby, this is the typical pattern for working with named, assignable memory storage. Variables are dynamic, mutable, and reassignable (with the exception of those defined with special restrictions such as Java’s final keyword).
In Scala, values are preferred over variables by convention, due to the stability and predictability they bring to source code. When you define a value you can be assured that it will retain the same value regardless of any other code that may access it. Reading and debugging code is easier when a value assigned at the beginning of a code segment is unchanged through the end of the code segment.
Finally, when working with data that may be available for the life span of an application, or accessible from concurrent or multithreaded code, an immutable value will be more stable and less prone to errors than mutable data that may be modified at unexpected times.
However, in those places where variables are more suitable, such as local variables that store temporary data or accumulate values in loops, variables will certainly be used. Now that the preference for values over variables has been explained in detail, we can put that aside and cover how to use variables in Scala.
The var keyword is used to define a variable with a given name, type, and assignment.
Syntax: Defining a Variable
- var <identifier>[: <type>] = <data>
- var <identifier>[: <type>] = <data>
Like values, variables can be defined with or without an explicit type. If no type is specified the Scala compiler will use type inference to determine the correct type to assign to your variable. Unlike values, variables can be reassigned new data at any time.
Here is an example of defining a variable and then reassigning it, in this case to the product of itself and another number:
- scala> var x = 5
- x: Int = 5
-
- scala> x = x * 4
- x: Int = 20
- scala> var x = 5
- x: Int = 5
-
- scala> x = x * 4
- x: Int = 20
Although a variable can be reassigned, its designated type cannot, and so a variable cannot be reassigned data that has an incompatible type. For example, defining a variable of type Int and then assigning it a String value will result in a compiler error:
- scala> var x = 5
- x: Int = 5
-
- scala> x = "what's up?"
- <console>:8: error: type mismatch;
- found: String("what\'s up?")
- required: Int x = "what's up?"
- ^
- scala> var x = 5
- x: Int = 5
-
- scala> x = "what's up?"
- <console>:8: error: type mismatch;
- found: String("what\'s up?")
- required: Int x = "what's up?"
- ^
However, defining a variable of type Double and assigning it an Int value will work because Int numbers can be converted to Double numbers automatically:
- scala> var y = 1.5
- y: Double = 1.5
-
- scala> y = 42
- y: Double = 42.0
- scala> var y = 1.5
- y: Double = 1.5
-
- scala> y = 42
- y: Double = 42.0
Share this article with your friends.
No comments :
Post a Comment