Re: [Vala] Constructors in VAPI



Abderrahim Kitouni wrote:
2009/9/22 Marco Trevisan <mail 3v1n0 net>:
Abderrahim Kitouni wrote:
did you try to set [CCode (cname=g_new0)] for the constructor? You can
make it work somehow with such tricks.

Yes, I tried but it doesn't work since it generates bad c code (with no
parameters for g_new0) like:
       _tmp0_ = g_new0 ();
Yes, I forgot to mention that you need to add default arguments to
make it work (IIRC, g_new0 takes a size, so you can make your
constructor

[CCode (cname = "g_new0")]
MyVirtualClass (ulong size = sizeof(int));

or something like this, the problem with this approach is that a user
can break his code (plus it's somewhat ugly)

Well, g_new0 takes the type and the number of instances to create, so it
would be hard using it. Maybe using something like malloc or calloc
whould not, but of course I don't like this approach.
Is too tricky.


With Vala 0.7.5 all this was possible, why isn't it any more?
It was a bug :-p

Seriously, I believe this is the intended behaviour, you declared a
*class* not a struct. A compact class' size is not always known, so it
doesn't  always make sense to use g_new, but it may make sense as a
fallback.

It's what I think too. The same, imho would be using g_free as default
when there's not a free_function (instead for using an invalid *_free).

I don't know if a struct is what you want or if you need a
constructor. How is this supposed to be used?

Well, in C I've some functions which should be used like this:

        int handle;
        function1(&handle, ...);
        function2(&handle, ...);
        ...
        functionN(&handle, ...);

These functions can be applied by the way to others handles returned by
other functions like:

        void* handle = ClassX.get_handle();

With Vala 0.7.5 I was able to define an abstract class Handle (with a
"void" real cname) that was used as returning type for all the class
functions which returned an handle. This class was extending a "virtual
class" MyClass which had an "int" as real cname and that included all
the functionX methods.

So I was able to:

        var handle1 = new MyClass();
        handle1.function1(<paramenters>);

but also:

        var handle2 = ClassX.get_handle();
        handle2.function2();

Now, at the contrary I can't do this at all. I figure the only way would
follow the "c way" by using an out int handle, but with that there won't
be any OOP.

Could please be added to Vala something that would allow to define types
in VAPI too (to workaround the C library lacks)?





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