Re: [Vala] First questions



Hi Zsombor

On Sam, 2006-07-29 at 12:41 +0200, Zsombor wrote:
 Nice language, it looks very interesting ! I've tried to create with it
a simple program, but I found some problem/question.

Thanks for testing.

 First, how the memory is managed? I suspect there is no garbage
collector. If it's true how can I release the objects? Ass I see no
explicit 'release' operation in the test programs.

Vala uses reference counting to manage memory of GObject instances. You
don't need to explicitly use g_object_ref / g_object_unref, though, as
the Vala compiler automatically inserts appropriate statements if you
correctly declare your fields and variables.

In Vala each field and each local variable - pointing to an object - is
either a strong or a weak reference. Strong reference variables work
automatically call g_object_ref and/or g_object_unref on assignments.
Weak references are necessary to get around the garbage cycle problem
that arises with reference counting - and they may improve performance.

Fields default to strong references while local variables default to
weak references to avoid the reference counting overhead.

To declare a field as a weak reference, you have to prepend the keyword
`weak' to the field type. To declare a local variable as a strong
reference, you have to prepend the keyword `ref' to the variable type.

Methods may transfer ownership of the return value to the caller. If
they do, you have to prepend the keyword `ref' to the return type to get
memory management right. If you assign the return value of a method that
transfers ownership to a local variable that is a weak reference, the
local variable automatically becomes a strong reference as a weak
reference wouldn't be allowed there.

This may sound a bit complicated compared to languages with integrated
garbage collector, it's quite simple, though, when you get used to it.

 The second, more practical question of mine is how to specify to
include Gtk? I wrote a simple program: 

using GLib;
using Gtk;

class HelloWorld {

      static int main (int argc, string[] argv) {
              var w = new Window();
              return 0;
      }

}

But I cant compile it with valac. No matter how I specify :
valac --vapidir=../vapi/ --pkg=Gtk --library=gtk-2.0.vala gtktest.vala

What's the expected format of --pkg=? and --library=? declarations? 

I've named the VAPI files like the corresponding pkg-config files, so
you have to use `--pkg=gtk+-2.0'. BTW: You only need to use --library if
you write a library, not if you use one; I will hopefully document all
this sometime soon. The GTK+ binding hasn't been shipped with the vala
0.0.1 tarball but you can get it from SVN. I also plan to release vala
0.0.2 relatively soon which will include the still incomplete GTK+
binding.

Don't hesitate to ask further questions, I know that the documentation
is missing.

Regards,

Jürg
-- 
Juerg Billeter <j bitron ch>




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