Vala (programming language)
Vala is an object-oriented programming language with a self-hosting compiler that generates C code and uses the GObject system.
Paradigm | Multi-paradigm: imperative, structured, object-oriented |
---|---|
Developer | Jürg Billeter, Raffaele Sandrini |
First appeared | 2006 |
Stable release | 0.50.2[1]
/ 19 November 2020 |
Preview release | 0.49.92
/ 4 September 2020 |
Typing discipline | static, strong |
OS | Cross-platform all supported by GLib, but distributed as source code only. |
License | LGPLv2.1+ |
Filename extensions | .vala, .vapi |
Website | wiki |
Influenced by | |
C, C++, C#, D, Java, Boo |
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
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 GLib;
interface Printable {
public abstract string print ();
public virtual string pretty_print () {
return "Please " + print ();
}
}
class NormalPrint: Object, Printable {
string print () {
return "don't forget about me";
}
}
class OverridePrint: Object, Printable {
string print () {
return "Mind the gap";
}
public override string pretty_print () {
return "Override";
}
}
void main (string[] args) {
var normal = new NormalPrint ();
var overridden = new OverridePrint ();
print (normal.pretty_print ());
print (overridden.pretty_print ());
}
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
- 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
- "Vala - Compiler Using the GObject Type System". GNOME Project. News section. Retrieved 17 December 2020.
- "Vala: high-level programming with less fat". Ars Technica. Retrieved 13 December 2011.
- "A look at two new languages: Vala and Clojure".
- "Writing Multimedia Applications with Vala". Archived from the original on 28 August 2012.
External links
Wikibooks has a book on the topic of: Vala Programming |
Wikimedia Commons has media related to Vala (programming language). |
- Official website
- ValaToWindows, Vala compiled binaries for Windows
- LibGee, a collection library for Vala.
- API Documentation
- Vala sample code for beginners
- List of Vala programs
- web-vala, a simple web application framework for Vala
- Autovala, a program that automatizes and simplifies creating CMake and Meson files for Vala/C projects
- VLS, Vala Language Server Protocol implementation in Vala
- GVLS, Another Vala Language Server Protocol implementation in Vala
- The Vala community on GitHub
- Comparison with other languages