Re: [Vala] Creating constructors on binded classes



On Sat, 2007-09-15 at 07:29 +0100, Alberto Ruiz wrote:
I have the following class from clutter:

    [CCode (copy_function = "clutter_knot_copy", cheader_filename =
"clutter/clutter.h")]
    public class Knot : GLib.Boxed {
        public int x; 
        public int y;
        public weak Clutter.Knot copy ();
        public bool equal (Clutter.Knot knot_b);
        public static GLib.Type get_type ();
    }

I don't really know why is it a class, since the structure 

Yes, it should probably be a struct, you should change that.

Obviously, in Vala, we want something more natural and comfortable
than:
var knot = new Knot();
knot.x = 1;
knot.y = 2;

Since usually, you have collections of several knots, in C you see the
use of this class as: 
Knot[] knots = {{1, 2}, {3, 4}, {5, 6} ...}

I would like to, at least, provide a simple constructor:
var knots = new Knot[] { new Knot(1, 2), new Knot(3, 4), ...}

It should be a bit better with the new object initializers. For example:
(untested)

Knot[] knots = new Knot[] { new Knot() {x=1, y=2}, ... };

The problem here is that if I create a new function, I need to copy
paste the whole class, hide it in metadata, paste it on
clutter-0.4-custom.vala and implement the new constructor. I don't
quite like this idea, is it possible to just add the constructor in
the custom file without getting the error that Knot is already
defined? 

Vala bindings currently don't allow any custom code at all, they are all
compile-time only (and without real code injection). I don't know yet
whether we want to add limited support for actual code in Vala bindings.
You can add a helper/factory method for creating Knot instances to your
application, of course.

Jürg




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