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@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@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to