[swift-evolution] Immutable Structures

Félix Cloutier felixcca at yahoo.ca
Wed Dec 23 10:28:00 CST 2015


The current choice is that if you need pass-by-value semantics, you use a struct; if you need pass-by-reference semantics, you use a class. This is almost only relevant when structs are mutable: the only semantic difference between a struct and a class is that mutating a struct won't have an impact on the original. If structs were immutable, there would be absolutely no distinction between a struct and a read-only class. In my opinion, immutable structs would make the choice much more ambiguous than it is right now.

Changing this would have a major impact. Arrays, dictionaries, sets and strings are all pass-by-value right now. It would be a massively breaking change to make them plainly immutable or to make them reference types. It would also throw a wrench into C interop, which is critical for Swift to succeed in a non-ObjC environment (like Linux).

I personally don't see a problem with the current semantics and I don't like the impact, so I'm not in favor of it.

Félix

> Le 23 déc. 2015 à 10:44:49, Lino Rosa via swift-evolution <swift-evolution at swift.org> a écrit :
> 
> 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
> 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/7163933b/attachment.html>


More information about the swift-evolution mailing list