This site has been retired. For up to date information, see handbook.gnome.org or gitlab.gnome.org.


[Home] [TitleIndex] [WordIndex

From:

Jürg Billeter

To:

Phil Housley

Subject:

Re: [Vala] Understanding object creation (and other things)

On Sam, 2007-04-21 at 23:54 +0100, Phil Housley wrote:

Welcome to Vala. I hope this mail will help you in understanding how it works and why it's done like that.

You can do this with construct-only properties without storing the value anywhere.

class SomeThing : Thing {
        public int x {
                construct {
                        // you can access the new value with `value'
                        // as in property setters in C#
                        initialize_things (value);
                }
        }

        // we call this a creation method
        // not guaranteed to be called
        // only allowed to set properties
        public SomeThing (construct int x) {
        }

        // that's how you can declare a constructor
        // where you can write arbitrary code that
        // is guaranteed to be called when creating
        // an instance of this class or a subclass
        construct {
        }
}

The construct in the parameter of the creation method is just a short version of setting the property x to the value of the argument x.

Yes, it can be very confusing if you're used to languages like Java or C#. The reason why Vala is that different is that it follows GObject. In Vala - and GObject in general - creation methods are not guaranteed to be called, for example the creation method of Thing won't be called when creating a new instance of SomeThing.

That's why we don't allow to write arbitrary code in creation methods as it could make it impossible to create a conforming subclass. If you have code that should always be executed, use a constructor as shown in the example above.

D uses its own object model so it's obviously not restricted to follow existing API and ABI as we are with GObject. The advantage of Vala is that it doesn't invent yet another object model and doesn't add yet another runtime to the many platforms we already have, it's just a language designed to leverage the existing GNOME platform.

Regards, Jürg


2024-10-23 11:37