Monday, May 13, 2019

Variable type difference between swift and javascript

Both javascript and swift use the keyword var to define variables, so it may give the impression the variable type works in similar way between swift and javascript.

However, there is a distinct difference between swift and javascript.

For javascript a variable defined by var can change its actual type any time, so when the variable is used in code, the type of the variable depends on the last assignment to the variable, so it may be a int, a string, or a function.
//valid for javascript, but invalid for swift
var i = 5;
i = "string"
i = null;

This is quite different for swift. In swift, from the moment a variable is declared or defined, it will have a particular type associated with it, and the in the whole life time of application running the type of this variable will not change, so whenever the variable is used in the code, we are certain the type of variable will be. As a result, the above code will not compile for Swift, as when i is first set to 5, its variable type is set to integer, and its type can never change, so assigning it to string or null will fail to compile.

Quite often, the confusion about swift variable type comes from type inference, in which case,  swift can infer the type of the variable from the context, so developers do not need to explicitly specify the variable type.

For example, both array and set use the same format to initialize the initial value, so without explicitly specify the variable type, developer may create a Array of string when a Set of string is expected.

        let arrVar = ["abc", "def"]
    let setVar : Set<String> = ["abc", "def"]

Another confusion of swift variable type is using closure. When closuring is used, swift complier can find out all the closure parameter types from dependent library, so it allows developer to skip those type information in the code. However, when people reading the code may not know the signature of the closure being invoked, so it will not give them the enough information when reading the code and understand the logic.

For example,  the following three ways of invocation will not show what input variable types being used in the closure, and when others read the code.


let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
reversedNames = names.sorted(by: >)
reversedNames = names.sorted(by: { $0 > $1 } )
reversedNames = names.sorted(by: { s1, s2 in s1 > s2 } )

While the below code will provide a better description to others about the closure signature being invoked.

     let reversedNames = names.sorted(by: {
          (s1 : String, s2 : String) -> Bool in
             return s1 > s2
     })

In addition, unique to Swift, when a value type is defined by let, the object instance, as well as all its properties cannot be changed. However, when a reference type is defined by let, although the object instance itself cannot be changed to another object instance, its properties can be changed to different values

No comments:

Post a Comment