Vala (programming language)

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

Vala
Vala Logo.svg
ParadigmMulti-paradigm: imperative, structured, object-oriented
DeveloperJürg Billeter, Raffaele Sandrini
First appeared2006; 13 years ago (2006)
Stable release
0.44.7[1] / 5 August 2019; 3 months ago (2019-08-05)
Preview release
0.46.0 / 5 September 2019; 2 months ago (2019-09-05)
Typing disciplinestatic, strong
OSCross-platform all supported by GLib, but distributed as source code only.
LicenseLGPLv2.1+
Filename extensions.vala, .vapi
Websitewiki.gnome.org/Projects/Vala
Influenced by
C, C++, C#, D, Java, Boo

Vala is an object-oriented programming language with a self-hosting compiler that generates C code and uses the GObject system.

Vala is syntactically similar to C# and includes notable features such as anonymous functions, signals, properties, generics, assisted memory management, exception handling, type inference, and foreach statements.[2] Its developers, Jürg Billeter and Raffaele Sandrini, wanted to bring these features to the plain C runtime with little overhead and no special runtime support by targeting the GObject object system. Rather than compiling directly to machine code or assembly language, it compiles to a lower-level intermediate language. It source-to-source compiles to C, which is then compiled with a C compiler for a given platform, such as GCC.[3]

For memory management, the GObject system provides reference counting. In C, a programmer must manually manage adding and removing references, but in Vala, managing such reference counts is automated if a programmer uses the language's built-in reference types rather than plain pointers.

Using functionality from native code libraries requires writing vapi files, defining the library interfacing. Writing these interface definitions is well-documented for C libraries, especially when based on GObject. However, C++ libraries are not supported. Vapi files are provided for a large portion of the GNOME platform, including GTK.

Vala was conceived by Jürg Billeter and was implemented by him and Raffaele Sandrini, finishing a self-hosting compiler in May 2006.[4]

Code example[edit]

A simple "Hello, World!" Vala program:

void main () {
    print ("Hello World\n");
}

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 ();
    }
}

Sample code to show Vala interface with default implementation(mixin)

using Gtk;
using Kangaroo.Illuminate.Contracts.Database;
using Kangaroo.Illuminate.Foundation;

namespace Kangaroo.Gui.Contracts 
{
    public interface IMetaLoader : GLib.Object {
        public abstract IConnection connection { get; }

        public virtual void fill_model_with_type(Gtk.ListStore model, DbMetaType meta_type)
        {
            Gtk.TreeIter tree_iter;
            GLib.Value cell_data_value;

            model.clear();
            model.append (out tree_iter);
            cell_data_value = GLib.Value(Type.STRING);
            cell_data_value.set_string("");
            model.set_value(tree_iter, 0, cell_data_value);

            string[] sql_meta_array = connection.inspector().support(meta_type);
            foreach (var meta_value in sql_meta_array) {
                model.append (out tree_iter);
                cell_data_value = GLib.Value(typeof(string));
                cell_data_value.set_string(meta_value);
                model.set_value(tree_iter, 0, cell_data_value);
            }
        }

        public virtual async void fill_model_with_sql(Gtk.ListStore model, string sql, int column = 0)
        {
            string str_value;
            Gtk.TreeIter tree_iter;
            GLib.Value cell_data_value;

            try {
                model.clear();
                model.append (out tree_iter);
                cell_data_value = GLib.Value(Type.STRING);
                cell_data_value.set_string("");
                model.set_value(tree_iter, 0, cell_data_value);

                Gda.DataModel data_model = yield connection.execute_as_model(sql);
                for (int index = 0; index < data_model.get_n_rows(); index++) {
                    str_value = data_model.get_value_at(column, index).get_string();

                    model.append (out tree_iter);
                    cell_data_value = GLib.Value(Type.STRING);
                    cell_data_value.set_string(str_value);
                    model.set_value(tree_iter, 0, cell_data_value);
                }
            } catch (Error e) {
                LoggingService.error(null, "query data failed: %s.", e.message);
            }
        }

        public virtual void fill_list_with_type(Gtk.ComboBoxText combobox, DbMetaType meta_type)
        {
            combobox.remove_all();
            combobox.append_text("");

            string[] sql_meta_array = connection.inspector().support(meta_type);
            foreach (var meta_value in sql_meta_array) {
                combobox.append_text(meta_value);
            }
        }

        public virtual async void fill_list_with_sql(Gtk.ComboBoxText combobox, string sql, int column = 0)
        {
            combobox.remove_all();
            combobox.append_text("");

            try {
                string str_value;
                Gda.DataModel data_model = yield connection.execute_as_model(sql);
                for (int index = 0; index < data_model.get_n_rows(); index++) {
                    str_value = data_model.get_value_at(column, index).get_string();
                    combobox.append_text(str_value);
                }
            } catch (Error e) {
                LoggingService.error(null, "query data failed: %s.", e.message);
            }
        }
    }
}


An example using GTK to create a GUI "Hello, World!" program (see also GTK hello world):

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 GNOME 3 platforms:

valac --pkg gtk+-3.0 hellogtk.vala

See also[edit]

  • Genie, a programming language for the Vala compiler with a syntax closer to Python.
  • Shotwell, an image organiser written in Vala.
  • Geary, an email client written in Vala.
  • Elementary OS, a Linux distribution with a desktop environment programmed mostly in Vala.

References[edit]

  1. ^ "Vala - Compiler Using the GObject Type System". GNOME Project. News section. Retrieved 5 September 2019.
  2. ^ "Vala: high-level programming with less fat". Ars Technica. Retrieved 13 December 2011.
  3. ^ "A look at two new languages: Vala and Clojure".
  4. ^ "Writing Multimedia Applications with Vala". Archived from the original on 28 August 2012.

External links[edit]

Comparison with other languages