Search code examples
gtkvalagenie

Gtk Hello World in Genie from Vala code


I wanted to reproduce a small Hello World Gtk program in Genie language, based in the following Vala code:

using Gtk;

int main(string[] args){
Gtk.init (ref args);
Gtk.Window window = new Gtk.Window (Gtk.WindowType.TOPLEVEL);
Gtk.Label label = new Gtk.Label("Hello world!");
window.add(label);
    window.set_default_size (300, 200);
    window.show_all ();
Gtk.main ();
return 0;
}

How would the same code look in Genie?


Solution

  • It would look like this:

    [indent=4]
    uses Gtk
    
    init
        Gtk.init (ref args)
        var window = new Window (WindowType.TOPLEVEL)
        var label = new Label("Hello world!")
        window.add(label)
        window.set_default_size (300, 200)
        window.show_all ()
        Gtk.main ()
    

    Note that args is implicitly available.

    I have used type inference when assigning the window and label identifiers. This is because the type is made clear on the right hand side.

    You can be explicit about the type by stating the type after the identifier. The identifier and type are separated by a colon:

        window:Window = new Window (WindowType.TOPLEVEL)
        label:Label = new Label ("Hello world!")

    Tabs are the default for indentation in Genie, but Stack Overflow insists on four and only multiples of four spaces. If you wish to use TABs for HTML examples of Genie code then the HTML TAB entity 	 works well. Even in the preview for Stack Overflow. In this example, however, [indent=4] has to be used at the beginning.