#2402 Field Self Assignment

SlimerDude Wed 11 Mar 2015

Hi, I came across the following edge case where I wanted to re-set a field to invoke the setter, but got a compiler Err instead:

class Example {
    Str? str {
        get { &str }
        set { &str = it; echo(it) }
    }
    
    Void main() {
        this.str = &str  // Compiler Err - Self assignment
    }
}

I can clearly see why the error exists, for it is a self assignment! But given the setter / use of field storage I was just wondering if the Err is warranted, or even if the compiler is able distinguish between the different types of set.

I'm not fussed either way (for the workaround is obvious!) - I just thought I'd throw it out for comment...

brian Thu 12 Mar 2015

Its an extremely valuable error check b/c things can get tricky with implicit this and it. Its also very nice if you use the same param name as fields in your constructors. I'd say it catches a bug or two a week for me. In your case, you just need to use a temp variable - that seems like a much rarer thing than the cases we want to catch at compile time.

Login or Signup to reply.