Ok, there was a bug in my code but the reaction of vala is curious. My code
did something like this:

public class Foo
{
    int i;

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

public class Bar
{
    Foo         f;

    public Bar()
    {
        assert( this != null );
        f.from(1);                         // this line causes the trouble
    }
}

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


This program fails in the following way:

**
ERROR:/net/tieto/home/vkirsan/sandbit/test.vala.c:305:bar_construct:
assertion failed: (this != null)
Abort


And vala generates the following interesting piece of code:

Bar* bar_construct (GType object_type) {
Bar* self = NULL;
Foo* _tmp0_ = NULL;
_vala_assert (self != NULL, "this != null");
_tmp0_ = self->priv->f;
self = (Bar*) foo_construct_from (object_type, 1);
return self;
}


So it looks like vala, compiling a constructor for a class, treats direct
calling constructor(s) of the object's field(s) as an instruction to
construct the object itself.

*Is it ok?* Or does it rather look like a bug?

Sorry, if I may not explain clearly enough, I'm no native speaker.


BR, Vitaly Kirsanov
skype: vkirsan

2014-11-11 13:52 GMT+03:00 Виталий Кирсанов <[email protected]>:

> Hm, the following code works fine indeed:
>
> public class Foo
> {
>     public Foo()
>     {
>         assert( this != null );
>     }
> }
>
> int main(string[] argv)
> {
>     var f = new Foo();
>     return 0;
> }
>
>
> But in my other real program a similar assertion is false in a
> constructor. I need dig to the bottom of it, will be back with results.
>
> BR, Vitaly Kirsanov
> skype: vkirsan
>
> 2014-11-11 3:37 GMT+03:00 Nor Jaidi Tuah <[email protected]>:
>
>>
>> > Is 'this' always null in constructors? The following code produces a
>> > run-time error:
>> >
>> > public class Foo
>> > {
>> >     Foo()
>> >     {
>> >         assert( this != null );
>> >     }
>> > }
>>
>> Are you sure about this? Perhaps you should
>> provide a small program that triggers this error.
>>
>> > Is there a way to get the true value of this in the constructor? I
>> guess,
>> > probably not. As far as I understand references in vala are somewhat
>> > similar to shared_ptr<> pointers in C++ and vala classes to
>> > enable_shared_from_this<> C++ subclasses. If the reasons are also
>> similar,
>> > it would be sad.
>>
>> Vala references are like C pointers -- raw,
>> unwrapped, direct and optimizer friendly (though
>> I notice that the vala compiler puts an
>> additional indirection for private variables,
>> incurring performance penalty in object construction
>> and subsequent accesses).
>>
>> C++ shared_ptr is an ugly wart. Its only similarity
>> with Vala references is ref counting.
>>
>> Do not let your experience with shared_ptr
>> discourage you from Vala.
>>
>> Nice day
>> Nor Jaidi Tuah
>>
>>
>>
>>
>> PRIVILEGED/CONFIDENTIAL information may be contained in this message. If
>> you are neither the addressee (intended recipient) nor an authorised
>> recipient of the addressee, and have received this message in error, please
>> destroy this message (including attachments) and notify the sender
>> immediately. STRICT PROHIBITION: This message, whether in part or in whole,
>> should not be reviewed, retained, copied, reused, disclosed, distributed or
>> used for any purpose whatsoever. Such unauthorised use may be unlawful and
>> may contain material protected by the Official Secrets Act (Cap 153) of the
>> Laws of Brunei Darussalam. DISCLAIMER: We/This Department/The Government of
>> Brunei Darussalam, accept[s] no responsibility for loss or damage arising
>> from the use of this message in any manner whatsoever. Our messages are
>> checked for viruses but we do not accept liability for any viruses which
>> may be transmitted in or with this message.
>> _______________________________________________
>> vala-list mailing list
>> [email protected]
>> https://mail.gnome.org/mailman/listinfo/vala-list
>>
>
>
_______________________________________________
vala-list mailing list
[email protected]
https://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to