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:

  • Hi, I'm pretty excited about how vala is coming on, but there are some things that I just don't follow. It's quite possible that I would understand a lot more if I knew more about glib/gobject, but I don't sadly. However, if vala is going to be adopted, it probably needs to appeal more to people like me, right?

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

  • The specific problem I have is trying to work out how to do something that's very simple in most languages. I want a constructor to take a parameter that is only of use in the constructor, and is never stored. Normally I would just:
    class SomeThing extends Thing {
        public SomeThing(int x) {
            initialize_things(x);
        }
    }
    But vala tells me that this constructor can't do anything except set member values. I'm sure this is a common thing to do, yet vala seems to make it very tricky. I don't want to store that "x" anywhere, just use it and forget about it.

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.

  • I guess overall I find the whole object construction thing very confusing. There seem to be 3 different ways to set the value of properties, and yet none of them can easily take arguments. This might all make sense if I knew more about glib, but I've tried to learn that and failed twice already.

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.

  • The other language I'm watching at the moment is D, which feels a lot more intuitive so far. I'd much rather use vala though, as it fits so nicely with everything else - gtk being a big selling point for me.

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

Projects/Vala/Syntax/ObjectConstruction (last edited 2013-11-22 16:48:31 by WilliamJonMcCann)