Assigning a variable to itself


(Saagar Jha) #1

It appears that trying to assign a variable to itself is an error:

var foo = 5
foo = foo // error: assigning a variable to itself

However, I was thinking about this and was wondering why this is an error and not a warning. If, for example, the assignment had a side effect (e.g. a didSet block) I’d think it would be warning, possibly ignorable with a set of parentheses. Thoughts?

Saagar Jha


(Fritz Anderson) #2

I’ll leave it to others to explain whether this is the intended behavior, but the further I get from properties being idempotent, the more grief I’ve had. Especially when (as I assume is your real use case) I tried to assign something to itself to force a didSet {} during viewDidLoad().

If I may read your mind further: Yes, your prepareForSegues will fill view controllers’ state before the view is instantiated, and therefore before its UI can be made to conform. Give in: Have the setter store the value in a private temporary, attempt to update its UI element through a conditional chain from its outlet (a no-op if the outlet isn’t filled yet), and have viewDidLoad() call a batch vars-to-UI func to bring everything up to the current state.

Of course there are side-effects; that’s what didSet {} is for. But assignment-to-self is more apt to be a typo for some other assignment than not; and it’s easy to work around by factoring the side effects into their own func — and then you can replace the self-assignment with an explicit call to the service you wanted to accomplish. Giving that service a name isolates it from additional tasks you might want to do at didSet {} time, and makes it clear at the point-of-call exactly what you want to do.

I can imagine an optimizer or runtime that assumes an assignment that doesn’t change the value can be skipped, but I’m sure it can’t be done without breaking some source. (Source which deserves breaking, but one picks one’s battles…)

  — F

···

On 21 Feb 2017, at 7:57 PM, Saagar Jha via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

It appears that trying to assign a variable to itself is an error:

var foo = 5
foo = foo // error: assigning a variable to itself

However, I was thinking about this and was wondering why this is an error and not a warning. If, for example, the assignment had a side effect (e.g. a didSet block) I’d think it would be warning, possibly ignorable with a set of parentheses. Thoughts?


(Fritz Anderson) #3

I’ll leave it to others to explain whether this is the intended behavior, but the further I get from properties being idempotent, the more grief I’ve had. Especially when (as I assume is your real use case) I tried to assign something to itself to force a didSet {} during viewDidLoad().

If I may read your mind further: Yes, your prepareForSegues will fill view controllers’ state before the view is instantiated, and therefore before its UI can be made to conform. Give in: Have the setter store the value in a private temporary, attempt to update its UI element through a conditional chain from its outlet (a no-op if the outlet isn’t filled yet), and have viewDidLoad() call a batch vars-to-UI func to bring everything up to the current state.

Of course there are side-effects; that’s what didSet {} is for. But assignment-to-self is more apt to be a typo for some other assignment than not; and it’s easy to work around by factoring the side effects into their own func — and then you can replace the self-assignment with an explicit call to the service you wanted to accomplish. Giving that service a name isolates it from additional tasks you might want to do at didSet {} time, and makes it clear at the point-of-call exactly what you want to do.

I can imagine an optimizer or runtime that assumes an assignment that doesn’t change the value can be skipped, but I’m sure it can’t be done without breaking some source. (Source which deserves breaking, but one picks one’s battles…)

  — F

···

On 21 Feb 2017, at 7:57 PM, Saagar Jha via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

It appears that trying to assign a variable to itself is an error:

var foo = 5
foo = foo // error: assigning a variable to itself

However, I was thinking about this and was wondering why this is an error and not a warning. If, for example, the assignment had a side effect (e.g. a didSet block) I’d think it would be warning, possibly ignorable with a set of parentheses. Thoughts?