Re: [swift-evolution] constant var

2017-12-14 Thread Joe Groff via swift-evolution


> On Dec 14, 2017, at 12:51 AM, Inder Kumar Rathore .  
> 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
>   }
> }

If you want code to be able to insert, but not delete from or otherwise freely 
change, the dictionary value, you could provide a wrapper type for myDict that 
provides a restricted mutation API:

struct InsertOnlyDictionary {
  private(set) var value = [String: String]()

  mutating func insert(_ k: String, _ v: String) {
value[k] = v
  }
}

// Code outside of InsertOnlyDictionary can read its `value`, but can only 
modify it by its `insert` API
class MyClass {
  private var myDict = InsertOnlyDictionary()

  func addMember() {
self.myDict.insert("key", "value")
  }
}

-Joe

___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] constant var

2017-12-14 Thread Magnus Ahltorp via swift-evolution

> 14 Dec. 2017 09:52 Inder Kumar Rathore . via swift-evolution 
>  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

___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] constant var

2017-12-14 Thread Nick Keets via swift-evolution
What are you going to do about this then?

func thirdFunc() {

self.myDict.removeAll()

}


Is this ok or not? Is it really different from `anotherFunc`?



On Thu, Dec 14, 2017 at 10:52 AM, Inder Kumar Rathore . via swift-evolution
 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
>
>   }
>
> }
>
> On Tue, Dec 12, 2017 at 10:28 PM, Joe Groff  wrote:
>
>>
>>
>> > On Dec 11, 2017, at 11:34 PM, Inder Kumar Rathore . via swift-evolution
>>  wrote:
>> >
>> > Hi All,
>> > Today I was writing code and faced a situation where I need to make a
>> instance variable a const i.e. it shouldn't accept new values from anywhere
>> but the problem is that I want it's content to be mutable.
>> >
>> > e.g.
>> >
>> > class MyClass {
>> >   var myDict = [String : String]()
>> > }
>>
>> You can do this by making the setter private:
>>
>> class MyClass {
>>   private(set) var myDict = [String: String]()
>> }
>>
>> This will allow declarations inside the MyClass definition to modify
>> myDict, but not code outside the class definition.
>>
>> -Joe
>>
>>
>
>
> --
> Best regards,
> Inder Kumar Rathore
>
> ___
> 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


Re: [swift-evolution] constant var

2017-12-14 Thread Magnus Ahltorp via swift-evolution

> 14 Dec. 2017 17:52 Inder Kumar Rathore . via swift-evolution 
>  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
>   }
> }

But in Swift, since myDict is a Dictionary, it is *one* value, so it doesn't 
make sense to allow one but not the other. Also, since it is one value, you 
will not be able to detect the difference between setting a part of a 
dictionary and the whole dictionary. If Dictionary had been a class, there 
would be a non-changing reference, but there isn't in this case. Conceptually, 
the whole dictionary is copied every time you set it or assign it.

/Magnus

___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] constant var

2017-12-14 Thread Inder Kumar Rathore . via swift-evolution
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

  }

}

On Tue, Dec 12, 2017 at 10:28 PM, Joe Groff  wrote:

>
>
> > On Dec 11, 2017, at 11:34 PM, Inder Kumar Rathore . via swift-evolution <
> swift-evolution@swift.org> wrote:
> >
> > Hi All,
> > Today I was writing code and faced a situation where I need to make a
> instance variable a const i.e. it shouldn't accept new values from anywhere
> but the problem is that I want it's content to be mutable.
> >
> > e.g.
> >
> > class MyClass {
> >   var myDict = [String : String]()
> > }
>
> You can do this by making the setter private:
>
> class MyClass {
>   private(set) var myDict = [String: String]()
> }
>
> This will allow declarations inside the MyClass definition to modify
> myDict, but not code outside the class definition.
>
> -Joe
>
>


-- 
Best regards,
Inder Kumar Rathore
___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] constant var

2017-12-12 Thread Joe Groff via swift-evolution


> On Dec 11, 2017, at 11:34 PM, Inder Kumar Rathore . via swift-evolution 
>  wrote:
> 
> Hi All,
> Today I was writing code and faced a situation where I need to make a 
> instance variable a const i.e. it shouldn't accept new values from anywhere 
> but the problem is that I want it's content to be mutable.
> 
> e.g.
> 
> class MyClass {
>   var myDict = [String : String]()
> }

You can do this by making the setter private:

class MyClass {
  private(set) var myDict = [String: String]()
}

This will allow declarations inside the MyClass definition to modify myDict, 
but not code outside the class definition.

-Joe

___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] constant var

2017-12-12 Thread BJ Homer via swift-evolution
If I understand correctly, you want to be able to modify myDict from within 
MyClass, but not from outside it. In that case, you’re looking for private(set).

class MyClass {
  private(set) var myDict = [String : String]()
}

-BJ

> On Dec 12, 2017, at 12:34 AM, Inder Kumar Rathore . via swift-evolution 
>  wrote:
> 
> Hi All,
> Today I was writing code and faced a situation where I need to make a 
> instance variable a const i.e. it shouldn't accept new values from anywhere 
> but the problem is that I want it's content to be mutable.
> 
> e.g.
> 
> class MyClass {
>   var myDict = [String : String]()
> }
> 
> 
> I want above variable to be constant and if I make it like below
> 
> class MyClass {
>   let myDict = [String : String]()
> }
> 
> Then I cann't add key/value in the myDict like
> 
>self.myDict["name"] = "Rathore"
> 
> 
> I know swift and couldn't find anything related to this.
> 
> Can anybody help me?
> 
> 
> If there is no such method of doing it then I would suggest to either use a 
> syntax like
> 
> class MyClass {
>   const var myDict = [String : String]()
> }
> 
> I'm not using final here since that make a var not overridable.
> 
> 
> 
> -- 
> Best regards,
> Inder Kumar Rathore
> ___
> 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


Re: [swift-evolution] constant var

2017-12-12 Thread Kelvin Ma via swift-evolution
here’s the problem. if your type is an inline type (ie a struct or an enum),
modifying a member of your object is really modifying part of the entire
variable. so, declaring the variable as let makes no sense. if your type is
an indirect type (a class), modifying a member of your object is really
modifying something that lives in the pointee of the variable. since the
variable itself is really a pointer, declaring it as let makes sense. you
can imagine class members as being accessed like -> in C, except by design
Swift does not need this operator so you can just use the dot to mutate
indirect members. personally i would just use a struct and declare the
variable as mutable as the costs of switching to a class type outweigh the
benefits if you ask me.

On Tue, Dec 12, 2017 at 2:00 AM, Inder Kumar Rathore . via swift-evolution <
swift-evolution@swift.org> wrote:

> Nice idea but I think the code will look ugly
>
> On Tue, Dec 12, 2017 at 1:28 PM, Rafael Guerreiro  > wrote:
>
>> You actually need a class to wrap the dictionary.
>> That’s because dictionaries are struct, with copy-on-write.
>>
>> With a class, you’ll be able to have it mutable, in a let declaration.
>> On Mon, Dec 11, 2017 at 11:34 PM Inder Kumar Rathore . via
>> swift-evolution  wrote:
>>
>>> Hi All,
>>> Today I was writing code and faced a situation where I need to make a
>>> instance variable a const i.e. it shouldn't accept new values from anywhere
>>> but the problem is that I want it's content to be mutable.
>>>
>>> e.g.
>>>
>>> class MyClass {
>>>   var myDict = [String : String]()
>>> }
>>>
>>>
>>> I want above variable to be constant and if I make it like below
>>>
>>> class MyClass {
>>>   let myDict = [String : String]()
>>> }
>>>
>>> Then I cann't add key/value in the myDict like
>>>
>>>self.myDict["name"] = "Rathore"
>>>
>>>
>>> I know swift and couldn't find anything related to this.
>>>
>>> Can anybody help me?
>>>
>>>
>>> If there is no such method of doing it then I would suggest to either
>>> use a syntax like
>>>
>>> class MyClass {
>>>   const var myDict = [String : String]()
>>> }
>>>
>>> I'm not using *final *here since that make a var not overridable.
>>>
>>>
>>>
>>> --
>>> Best regards,
>>> Inder Kumar Rathore
>>> ___
>>> swift-evolution mailing list
>>> swift-evolution@swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>>
>>
>
>
> --
> Best regards,
> Inder Kumar Rathore
>
> ___
> 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


Re: [swift-evolution] constant var

2017-12-12 Thread Austin Zheng via swift-evolution
Ugliness is preferable to the alternative of impossibility because what you are 
trying to do and the semantics of the type you are trying to do it with are 
irreconcilably and mutually exclusive.

> On Dec 12, 2017, at 12:00 AM, Inder Kumar Rathore . via swift-evolution 
>  wrote:
> 
> Nice idea but I think the code will look ugly
> 
> On Tue, Dec 12, 2017 at 1:28 PM, Rafael Guerreiro  > wrote:
> You actually need a class to wrap the dictionary.
> That’s because dictionaries are struct, with copy-on-write.
> 
> With a class, you’ll be able to have it mutable, in a let declaration.
> On Mon, Dec 11, 2017 at 11:34 PM Inder Kumar Rathore . via swift-evolution 
> > wrote:
> Hi All,
> Today I was writing code and faced a situation where I need to make a 
> instance variable a const i.e. it shouldn't accept new values from anywhere 
> but the problem is that I want it's content to be mutable.
> 
> e.g.
> 
> class MyClass {
>   var myDict = [String : String]()
> }
> 
> 
> I want above variable to be constant and if I make it like below
> 
> class MyClass {
>   let myDict = [String : String]()
> }
> 
> Then I cann't add key/value in the myDict like
> 
>self.myDict["name"] = "Rathore"
> 
> 
> I know swift and couldn't find anything related to this.
> 
> Can anybody help me?
> 
> 
> If there is no such method of doing it then I would suggest to either use a 
> syntax like
> 
> class MyClass {
>   const var myDict = [String : String]()
> }
> 
> I'm not using final here since that make a var not overridable.
> 
> 
> 
> -- 
> Best regards,
> Inder Kumar Rathore
> ___
> swift-evolution mailing list
> swift-evolution@swift.org 
> https://lists.swift.org/mailman/listinfo/swift-evolution 
> 
> 
> 
> 
> -- 
> Best regards,
> Inder Kumar Rathore
> ___
> 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


Re: [swift-evolution] constant var

2017-12-12 Thread Inder Kumar Rathore . via swift-evolution
Nice idea but I think the code will look ugly

On Tue, Dec 12, 2017 at 1:28 PM, Rafael Guerreiro 
wrote:

> You actually need a class to wrap the dictionary.
> That’s because dictionaries are struct, with copy-on-write.
>
> With a class, you’ll be able to have it mutable, in a let declaration.
> On Mon, Dec 11, 2017 at 11:34 PM Inder Kumar Rathore . via swift-evolution
>  wrote:
>
>> Hi All,
>> Today I was writing code and faced a situation where I need to make a
>> instance variable a const i.e. it shouldn't accept new values from anywhere
>> but the problem is that I want it's content to be mutable.
>>
>> e.g.
>>
>> class MyClass {
>>   var myDict = [String : String]()
>> }
>>
>>
>> I want above variable to be constant and if I make it like below
>>
>> class MyClass {
>>   let myDict = [String : String]()
>> }
>>
>> Then I cann't add key/value in the myDict like
>>
>>self.myDict["name"] = "Rathore"
>>
>>
>> I know swift and couldn't find anything related to this.
>>
>> Can anybody help me?
>>
>>
>> If there is no such method of doing it then I would suggest to either use
>> a syntax like
>>
>> class MyClass {
>>   const var myDict = [String : String]()
>> }
>>
>> I'm not using *final *here since that make a var not overridable.
>>
>>
>>
>> --
>> Best regards,
>> Inder Kumar Rathore
>> ___
>> swift-evolution mailing list
>> swift-evolution@swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>


-- 
Best regards,
Inder Kumar Rathore
___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] constant var

2017-12-11 Thread Rafael Guerreiro via swift-evolution
You actually need a class to wrap the dictionary.
That’s because dictionaries are struct, with copy-on-write.

With a class, you’ll be able to have it mutable, in a let declaration.
On Mon, Dec 11, 2017 at 11:34 PM Inder Kumar Rathore . via swift-evolution <
swift-evolution@swift.org> wrote:

> Hi All,
> Today I was writing code and faced a situation where I need to make a
> instance variable a const i.e. it shouldn't accept new values from anywhere
> but the problem is that I want it's content to be mutable.
>
> e.g.
>
> class MyClass {
>   var myDict = [String : String]()
> }
>
>
> I want above variable to be constant and if I make it like below
>
> class MyClass {
>   let myDict = [String : String]()
> }
>
> Then I cann't add key/value in the myDict like
>
>self.myDict["name"] = "Rathore"
>
>
> I know swift and couldn't find anything related to this.
>
> Can anybody help me?
>
>
> If there is no such method of doing it then I would suggest to either use
> a syntax like
>
> class MyClass {
>   const var myDict = [String : String]()
> }
>
> I'm not using *final *here since that make a var not overridable.
>
>
>
> --
> Best regards,
> Inder Kumar Rathore
> ___
> 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