[swift-evolution] constant var

Kelvin Ma kelvin13ma at gmail.com
Tue Dec 12 02:38:19 CST 2017


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 at swift.org> wrote:

> Nice idea but I think the code will look ugly
>
> On Tue, Dec 12, 2017 at 1:28 PM, Rafael Guerreiro <guerreiro.dev at gmail.com
> > 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 <swift-evolution at 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 at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>>
>>
>
>
> --
> Best regards,
> Inder Kumar Rathore
>
> _______________________________________________
> 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/20171212/f1b99fda/attachment.html>


More information about the swift-evolution mailing list