*Paul Cantrell:* *> “Ah, OK, sure … but why is this a problem? You changed the declaration! Of course it behaves differently. Changing a declaration can transparently change behavior in any number of circumstances. The following code still compiles if you change “Int” to “UInt8,” but it crashes:”*
Good point, but I still think this is on a different class of problem because 1) it’s really hard to track where we were relying on value semantics before and adapt it to using classes and 2) the sort of bugs that would creep out wouldn’t probably be crashes but weird side-effects incredibly hard to track. *David Owens II:* > *"Did you mean “var” there? (sidenote: var is being removed from the function call site)"* Yes I meant it, or it won’t compile. You’re right, they’re equivalent. Oh the `var` is being deprecated there? Nice to know :) > *"I agree that this type of refactoring is inherently fragile, but I don’t see how having “immutable” structs changes this."* It would solve it (although quite indirectly) because we couldn’t have had code that mutated the structure before the said refactoring. In other words, since we weren’t relying on the value semantics before (because of immutability) then refactoring to classes wouldn’t be fragile. *Rudolf Adamkovič:* > *"Isn’t there a better keyword (than “mutating”) that we could use here?"* I don’t understand why we need the `mutating` keyword in the first place. Is it for readability? Right now the compiler knows when a function mutates a property and forces us to write `mutating`. Couldn’t it just fail compiling when try to call a mutation function but we’re not allowed? Douglas Gregor: I realize there’s substantial personal opinion involved in this - and I’m vastly outnumbered here :) Still, I’m replying with my thoughts after watching your presentation. Maybe that clarifies some of my points and, who knows, even increases my odds to 1%? -- The temperature class example -- You make a point of how shared references sometimes result unintended consequences. However, they’re only unintended because the fictional developer in the example is using classes and expects value semantics. Couldn’t one make the opposite argument, where the developer expected reference semantics while using structures? -- Immutability sometimes leads to awkward interfaces This is the "awkward" version from the video: `home.oven.temperature = Temperature(fahrenheit: temp.fahrenheit + 10.0)` You could make it not awkward by implementing `+` and `-` in the global scope so it adds numbers and `Temperature` together. This would bring it back to: `home.oven.temperature = temp + 10.0`. You already do it with strings: `let foobar = "Foo" + "Bar"`. By the way, structures as value types (arguably) create some awkwardness and certainly add special cases to the language 1) `let` and `var` aren’t just about reassignment anymore. It depends on the context: for classes it’s still about reassignment; for structures it’s about reassignment and mutability (well, also depending on whether the properties of the structure are `let` or `var` themselves). 2) the `mutating` keyword on protocol extensions is only there for structures and enums - and doesn’t even enforce them to actually mutate anything. 3) the class specific protocol extensions. -- Immutability does not map efficiently to the machine model -- Yes, but that’s only an issue if the language is 100% immutable. -- Integers are value types -- Aren’t they also immutable? -- Arrays are value types -- Why? I would honestly like to know. I think all languages I’ve used in the past had arrays as reference types. Is it just because of equality? -- CGPoints are value types -- Other than digging into the documentation, how can we tell it has value semantics? We can’t just skim the code and understand it anymore - we have to dig into class/structure definitions to figure out whether each thing is a value or reference. And we’ll have to do it constantly because things that work with structures would be disastrous for classes. -- Freedom from race conditions -- Only accidental race conditions. Often memory is shared because it must be. Those are the tricky ones to solve. -- Reference types as properties of structures -- Why even allow it? Isn’t the whole point of structures to be used in small, simple cases? Allowing reference types inside opens up a can of worms with these “forReading/forWriting” methods… Finally, thanks for the link to the video Douglas. I hope I didn’t sound combative. It wasn’t my intention. Sadly I wasn't converted. Perhaps next year? :) On Wed, Dec 23, 2015 at 6:13 PM Matthew Johnson <[email protected]> wrote: > > > Sent from my iPad > > > On Dec 23, 2015, at 3:57 PM, Douglas Gregor via swift-evolution < > [email protected]> wrote: > > > > > >> On Dec 23, 2015, at 7:44 AM, Lino Rosa via swift-evolution < > [email protected]> wrote: > >> > >> I believe the language would be improved by making structures immutable. > >> > >> 1) The choice between classes and structures isn’t clear right now. If > structures were immutable it would be natural to use them as value objects. > >> > >> 2) Refactoring a mutable structure into a class when it’s being passed > around multiple threads removes the by-value semantics seamlessly. The > resulting mutable class isn’t thread-safe. > >> > >> 2.1) Even when passed around a single thread, the resulting class would > be passed by reference, so any mutations would have unintended consequences. > >> > >> 3) We could probably remove some syntax: `mutating` keyword and > variable parameters. Also the `var` keyword before a structure could be > used to denote reassignment (not mutability), just as it does with classes. > >> > >> Of corse I might not be seeing the whole picture, so please weigh in. > > > > Swift’s structures, enums, and standard library collections provide > value semantics, which is far more useful than true immutability because it > gives you immutability guaranteed when you want them (“let”) and efficient > local mutation when you need it (“var”) . If you haven’t seen the “Building > Better Apps with Value Types in Swift” talk from WWDC 2015, I suggest you > check it out to get a sense of how value types work in Swift: > > > > https://developer.apple.com/videos/play/wwdc2015-414/ > > > > We’re very happy with value semantics as a programming model, so the > likelihood of moving to a model where a value of struct type is always > immutable is effectively zero. My hope is that the talk above—or other > resources about value semantics in Swift—will convince you as thoroughly as > it convinced us ;) > > +1,000,000. This is one of the best aspects of Swift IMO. > > > > > > - Doug > > > > > > _______________________________________________ > > swift-evolution mailing list > > [email protected] > > https://lists.swift.org/mailman/listinfo/swift-evolution >
_______________________________________________ swift-evolution mailing list [email protected] https://lists.swift.org/mailman/listinfo/swift-evolution
