[swift-evolution] [proposal] Allow "let" for computed properties which only reference immutable data

Rod Brown rodney.brown6 at icloud.com
Wed May 11 10:34:35 CDT 2016


I believe the reason behind this comes down to the fact that computed values generally won't be used in this way - thus the "computed".

var itself isn't a promise that a value is mutable, so much as 'let' is a promise of immutability. If we allowed this, then the compiler must check that each element of the return value is static and will not change, and in that case, why would you not just store the computer value in a true let to begin with, to avoid doing the work over again on each access?

It seems a somewhat rare and complicated case for the compiler, for little if any benefit.

-Rod

> On 11 May 2016, at 11:25 PM, Alexander Momchilov via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I came an interesting SO question which pointed out a strange quirk: a computed property must always use the "var" keyword, even if it's read-only, and only referencing other immutable data.
> 
> class Test {
>   let hello = "hello"
>   let world = "world"
>   var phrase: String { //why must this be 'var'?
>      return self.hello + self.world
>   }
> }
> It would be more appropriate for such a read-only, immutable property, to use the "let" syntax, so that its immutability is correctly expressed.
> 
> Thoughts?
> _______________________________________________
> 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/20160512/b9f37bd6/attachment.html>


More information about the swift-evolution mailing list