Re: [Vala] mandatory this. in classes?



2011/1/17 Pavol Klačanský <pavol klacansky com>:
Hi, why works this?

public class Test {
       public int test;

       public Test() {

               stderr.printf("%d", test);
       }
}

why is not "this." preffix mandatory?

Basically because it doesn't need to be. Variables are searched for in
various scopes in a set order, in this case: first the local (method)
scope, then the current instance (this) scope. Since that finds the
correct name, and there is no ambiguity, it works. If it hadn't found
anything there are more scopes to search, up to the global scope.
Using "this" keyword just makes it explicit what you mean, and so lots
of people do that to be safe/clear.

-- 
Phil Housley



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]