Re: [Vala] not null 'this' in constructors



On 11/11/2014 09:17 AM, Виталий Кирсанов wrote:
f.from(1);                         // this line causes the trouble
Your syntax is incorrect. This line should be:

f = new Foo.from (1);

The way you have written it, vala thinks Bar inherits from Foo. In the case where you call a base constructor, this will indeed be null until you call the base constructor.

public class Foo
{
    int i;

    public Foo.from(int i)
    {
        this.i = i;
    }
}

public class Bar : Foo
{
    public Bar()
    {
        assert( this != null ); // fails because `this` really is null here
        base.from(1);
        assert( this != null ); // succeeds
    }
}

int main(string[] argv)
{
    var b = new Bar();
    return 0;
}




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