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.
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.
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 <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.
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
···
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]()
}
On Tue, Dec 12, 2017 at 1:28 PM, Rafael Guerreiro <guerreiro.dev@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@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.
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 <jgroff@apple.com> 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.
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 <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 <guerreiro.dev@gmail.com <mailto:guerreiro.dev@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@swift.org <mailto: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 <mailto: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 <mailto:swift-evolution@swift.org> https://lists.swift.org/mailman/listinfo/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 <guerreiro.dev@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@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.
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]()
// Code outside of InsertOnlyDictionary can read its `value`, but can only modify it by its `insert` API
class MyClass {
private var myDict = InsertOnlyDictionary()
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.
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 <swift-evolution@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
}
}
On Tue, Dec 12, 2017 at 10:28 PM, Joe Groff <jgroff@apple.com> 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.