Posts

Showing posts with the label mixed semantics

Treehouse Intermediate Course Part 5 (Semantics)

Here we go! Haven't had the chance over the past two days so am going to do a good half an hour or so now! Last time, it was a big focus on initialisers. I've made of sense of those and hopefully I'll be able to apply that. Now we're moving on to something else - can't remember. Anyway, let's go! Value Semantics Value types - structs, enums etc. struct Point {          var x: Double     var y: Double } var p1 = Point (x: 1 , y: 2 ) var p2 = p1 If we change the x value of p1, then let's see what happens to p2. It does not change!  A value type is COPIED ON ASSIGNMENT! We are copying the underlying value.  let p3 = Point (x: 2 , y: 4 ) Here, this constant is IMMUTABLE. You cannot then change the values! However with var (p1 or p2) you can change values, as long as the stored properties are variables (which they are!) struct AnotherPoint {          let x: Doubl...