Scala uses static typing |
The biggest difference between Python and Scala is that Python uses dynamic typing, Scala uses static typing.
Every piece of data handled by a Python or Scala program is an object. Every object has a type. The type of an object determines what we can do with the object. Examples of objects are numbers, strings, files, and digital images.
A variable is a name assigned to an object during the execution of a program. The object currently assigned to a name is called the value of the name or variable.
In dynamically typed programming languages like Python, the same name can be assigned to all kinds of different objects:
# This is Python! m = 17 # int m = "seventeen" # str m = 17.0 # floatThis is flexible, and makes it easy to quickly write some code. It also makes it easy to make mistakes, though. If you assign an object of the incorrect type, you only find out during the execution of the program that something doesn't work—you get a runtime error.
Scala is a statically typed language. This means that a variable can only be assigned to objects of one fixed type, the type of the variable:
var m : Int = 17 m = 18 // ok m = "seventeen" // error! m = 18.0 // error!
The advantage of a statically typed language is that the compiler can catch type errors during the compilation, before the program is even run. A good compiler can also generate more efficient code for a statically type language, as the type of objects is already known during the compilation. Consider the following Python function:
# This is Python! def test(a, b): print(a + b)The + operator here could mean integer addition, float addition, string concatenation, or list concatenation, depending on the type of the parameters a and b. The code created for this function must look at the types of the arguments and determine which method to call. Now consider the following Scala function:
def test(a : Int, b : Int) { println(a + b) }Here it is clear to the compiler that the + operator means integer addition, and so the code for the function can immediately add the two numbers.
Other statically typed languages are Java, C, and C++. One disadvantage of statically typed languages is that one has to write type names everywhere, leading to code that is much more verbose than code in, say, Python. Indeed, in languages like Java or C, the programmer has to write down the type for every variable she uses:
/* This is C */ int m = 17; float f = 17.0;
Scala makes our life easier and our code shorter by using type inference. If Scala can detect what the type of a variable name should be, then we do not need to indicate the type:
scala> var m : Int = 17 // ok m: Int = 17 scala> var n = 18 // also ok! n: Int = 18 scala> var f = 19.5 f: Double = 19.5 scala> var h = "Hello World" h: java.lang.String = Hello WorldNote how the interactive Scala interpreter tells you what the type of the name is even though we have not indicated it at all. When you are not sure if it is okay to omit the type of a name, you can always write it.
Scala uses static typing |