[swift-evolution] Immutable Structures

Rudolf Adamkovič salutis at me.com
Wed Dec 23 15:36:54 CST 2015


BTW, structures are immutable yet they can have mutating functions.

I fully understand how this works as a language user but I wonder...

Isn’t there a better keyword (than “mutating”) that we could use here?

R+

> On 23 Dec 2015, at 16:51, Ian Ynda-Hummel via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I thought structures were immutable and mutating functions actually just created new structures, thus maintaining the by value semantics.
> On Wed, Dec 23, 2015 at 10:45 AM Lino Rosa via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> 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-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>  _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151223/e3169324/attachment.html>


More information about the swift-evolution mailing list