[swift-evolution] Immutable Structures
Wallacy
wallacyf at gmail.com
Fri Dec 25 17:00:38 CST 2015
*"To me documentation plus the type signatures are generally enough.
However I could probably still agree with you if mutating was also used in
classes. What's the point of having it only on structures?"*
Because, structs ARE immutable by default. The mutating keyword says to
holding variable to copy-change and re-assign the value type. You never
notice the didSet called after mutation?
*"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."*
1) Its not true. Its just about the reassignment. But for value-types in
automatic the process of copy-change and assign.
2) because only value types need this keyword. And the problem of do not
enforce the mutation maybe can be a future improvement, it's not a big
deal, you can propose this adjustment.
3) what's the problem here?
*"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."*
Can you elaborate this? Does not appear a real world problem.
Change struct for class will always be a "break change". Its not free of
bugs in any language.
*"-- Integers are value types --*
*Aren’t they also immutable?"*
yes....
*"-- 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? *
*"*
This ship sailed a long time ago.... This is a big feature in swift, on the
old swift forum may you can see the role discussion about that.
Value type arrays are something with I miss in other languages.
*"-- 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."*
hu? Just check if the type is a struct or a enum! So will be a value type.
Whats the problem here?
*"-- Freedom from race conditions --*
*Only accidental race conditions. Often memory is shared because it must
be. Those are the tricky ones to solve.*
*"*
So, "accidental" are the name of most common bugs right? All bugs are
"accidental".... The same argument you can use for avoid Optionals.
*"-- 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…*
*"*
No, the whole point of structs are not to be used in small and simple
cases, not on Swift, in swift um Swift are first class citizen and can be
use for complex data structure, like Array and Strings. You can make a
private shared buffer behind the scenes.
"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? :)
"
FWIW:
Try this one: https://developer.apple.com/videos/play/wwdc2015-408/ (
Protocol-Oriented
Programming in Swift )
It explains (too) why structs are so important in Swift. Spoiler: Because
its not a OO primary language.
Em sex, 25 de dez de 2015 às 15:47, Lino Rosa via swift-evolution <
swift-evolution at swift.org> escreveu:
>
> On Fri, Dec 25, 2015 at 12:32 T.J. Usiyan <griotspeak at gmail.com> wrote:
>
>>
>>>
>>> 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?
>>>
>>>
>> I strongly disagree with this sentiment. I agree that the compiler can
>> figure it out without us in many cases but I appreciate knowing
>> *explicitly* which parts of an API will mutate a value. It is helpful for
>> both the vendor and consumer of the API to require that keyword.
>>
>> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151225/7268d235/attachment.html>
More information about the swift-evolution
mailing list