[swift-evolution] constant var

Magnus Ahltorp map at kth.se
Thu Dec 14 03:53:40 CST 2017


> 14 Dec. 2017 09:52 Inder Kumar Rathore . via swift-evolution <swift-evolution at swift.org> wrote:
> 
> class MyClass {
>   private var myDict = [String : String]()
>   
>   func addMemebr() {
>     self.myDict["key"] = "value" // Ok for me
>   }
>   
>   func anotherFunc() {
>     self.myDict = [String : String]() // Not okay for me, I don't want any code to do this within the class
>   }
> }

Let me take another example, if we write this extension to Int:

extension Int {
    subscript(bit: Int) -> Bool {
        get {
            return get_bit_value(self, bit)
        }
        set {
           self = set_bit_value(self, bit, newValue)
        }
    }
}

this wouldn't be meaningful, right?

class MyClass {
  private var myInt : Int = 0
  
  func addMember() {
    self.myInt[10] = true // Ok for me
  }
  
  func anotherFunc() {
    self.myInt = 3 // Not okay for me, I don't want any code to do this within the class
  }
}

There is no conceptual difference between Dictionary and Int here.

/Magnus



More information about the swift-evolution mailing list