Vala (programming Language) - Code Example

Code Example

A simple "Hello, World!" program:

int main { print ("Hello World\n"); return 0; }

A more complex version, showing some of Vala's object-oriented features:

class Sample : Object { void greeting { stdout.printf ("Hello World\n"); } static void main (string args) { var sample = new Sample ; sample.greeting ; } }

An example using GTK+ to create a GUI "Hello, World!" program:

using Gtk; int main (string args) { Gtk.init (ref args); var window = new Window ; window.title = "Hello, World!"; window.border_width = 10; window.window_position = WindowPosition.CENTER; window.set_default_size(350, 70); window.destroy.connect (Gtk.main_quit); var label = new Label ("Hello, World!"); window.add (label); window.show_all ; Gtk.main ; return 0; }

The last example needs an extra parameter to compile on GNOME3 platforms:

valac --pkg gtk+-3.0 hellogtk.vala

This is the converted C code:

/* hellogtk.c generated by valac 0.20.1, the Vala compiler * generated from hellogtk.vala, do not modify */ #include #include #include #include #include #define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL))) gint _vala_main (gchar** args, int args_length1); static void _gtk_main_quit_gtk_widget_destroy (GtkWidget* _sender, gpointer self); static void _gtk_main_quit_gtk_widget_destroy (GtkWidget* _sender, gpointer self) { gtk_main_quit ; } gint _vala_main (gchar** args, int args_length1) { gint result = 0; GtkWindow* _tmp0_; GtkWindow* window; GtkLabel* _tmp1_; GtkLabel* label; gtk_init (&args_length1, &args); _tmp0_ = (GtkWindow*) gtk_window_new (GTK_WINDOW_TOPLEVEL); g_object_ref_sink (_tmp0_); window = _tmp0_; gtk_window_set_title (window, "Hello, World!"); gtk_container_set_border_width ((GtkContainer*) window, (guint) 10); g_object_set (window, "window-position", GTK_WIN_POS_CENTER, NULL); gtk_window_set_default_size (window, 350, 70); g_signal_connect ((GtkWidget*) window, "destroy", (GCallback) _gtk_main_quit_gtk_widget_destroy, NULL); _tmp1_ = (GtkLabel*) gtk_label_new ("Hello, World!"); g_object_ref_sink (_tmp1_); label = _tmp1_; gtk_container_add ((GtkContainer*) window, (GtkWidget*) label); gtk_widget_show_all ((GtkWidget*) window); gtk_main ; result = 0; _g_object_unref0 (label); _g_object_unref0 (window); return result; } int main (int argc, char ** argv) { g_type_init ; return _vala_main (argv, argc); }

Read more about this topic:  Vala (programming Language)

Famous quotes containing the word code:

    “... the self respect of individuals ought to make them demand of their leaders conformity with an agreed-upon code of ethics and moral conduct.”
    —Mary Barnett Gilson (1877–?)

    “Wise Draco comes, deep in the midnight roll
    Of black artillery; he comes, though late;
    In code corroborating Calvin’s creed
    And cynic tyrannies of honest kings;
    He comes, nor parlies; and the Town, redeemed,
    Gives thanks devout; nor, being thankful, heeds
    The grimy slur on the Republic’s faith implied,
    Which holds that Man is naturally good,
    And—more—is Nature’s Roman, never to be
    scourged.”
    —Herman Melville (1819–1891)