Vala (programming language)

Last updated

Vala
Vala Logo.svg
Paradigm Multi-paradigm: imperative, structured, object-oriented
Developer Jürg Billeter, Raffaele Sandrini
First appeared2006;18 years ago (2006)
Stable release
0.57.0  OOjs UI icon edit-ltr-progressive.svg / 11 April 2023;11 months ago (11 April 2023)
Typing discipline Static, strong, inferred, structural
OS Cross-platform all supported by GLib, but distributed as source code only.
License LGPLv2.1+
Filename extensions .vala, .vapi
Website vala.dev
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.

Contents

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. [1] 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 or Clang. [2]

Using functionality from native code libraries requires writing vapi files, defining the library interfaces. Writing these interface definitions is well-documented for C libraries. Bindings are already available for a large number of libraries, including libraries that are not based on GObject such as the multimedia library SDL and OpenGL.

Description

Vala is a programming language that combines the high-level build-time performance of scripting languages with the run-time performance of low-level programming languages. It aims to bring modern programming language features to GNOME developers without imposing any additional runtime requirements and without using a different ABI, compared to applications and libraries written in C. The syntax of Vala is similar to C#, modified to better fit the GObject type system. [3]

History

Vala was conceived by Jürg Billeter and was implemented by him and Raffaele Sandrini, who wished for a higher level alternative for developing GNOME applications instead of C. They did like the syntax and semantics of C# but did not want to use Mono, so they finished a compiler in May 2006. Initially, it was bootstrapped using C, and one year later (with release of version 0.1.0 in July 2007), the Vala compiler became self-hosted. As of 2021, the current stable release branch with long-term support is 0.48, and the language is under active development with the goal of releasing a stable version 1.0. [4]

VersionRelease date [5]
Old version, no longer maintained: 0.0.12006-07-15
Old version, no longer maintained: 0.1.02007-07-09
Old version, no longer maintained: 0.10.02010-09-18
Old version, no longer maintained: 0.20.02013-05-27
Old version, no longer maintained: 0.30.02015-09-18
Old version, no longer maintained: 0.40.02018-05-12
Old version, no longer maintained: 0.42.02018-09-01
Old version, no longer maintained: 0.44.02019-05-09
Old version, no longer maintained: 0.46.02019-09-05
Old version, no longer maintained: 0.48.02020-03-03
Old version, no longer maintained: 0.50.02020-09-10
Old version, no longer maintained: 0.52.02021-05-17
Old version, no longer maintained: 0.54.02021-09-16
Current stable version: 0.56.142023-11-12
Legend:
Old version
Older version, still maintained
Latest version
Latest preview version
Future release
For old versions, only first point releases are listed

Language design

Features

Vala uses GLib and its submodules (GObject, GModule, GThread, GIO) as the core library, which is available for most operating systems and offers things like platform independent threading, input/output, file management, network sockets, plugins, regular expressions, etc. The syntax of Vala currently supports modern language features as follows:

Graphical user interfaces can be developed with the GTK GUI toolkit and the Glade GUI builder.

Memory management

For memory management, the GType or 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. The only detail you need to worry about is to avoid generating reference cycles, because in that case this memory management system will not work correctly. [6]

Vala also allows manual memory management with pointers as an option.

Bindings

Vala is intended to provide runtime access to existing C libraries, especially GObject-based libraries, without the need for runtime bindings. To use a library with Vala, all that needed is an API file (.vapi) containing the class and method declarations in Vala syntax. However, C++ libraries are not supported. At present, vapi files for a large part of the GNU project and GNOME platform are included with each release of Vala, including GTK. There is also a library called Gee, written in Vala, that provides GObject-based interfaces and classes for commonly used data structures. [7]

It should also be easily possible to write a bindings generator for access to Vala libraries from applications written in other languages, e.g., C#, as the Vala parser is written as a library, so that all compile-time information is available when generating a binding.

Tools

Editors

Tooling for Vala development has seen significant improvement over the recent years. The following is a list of some popular IDEs and text editors with plug-ins that add support for programming in Vala:

Code intelligence

Currently, there are two actively developing language servers which offer code intelligence for Vala as follows:

Build systems

Currently, there are a number of build systems supporting Vala, including Automake, CMake, Meson, and others. [13]

Debugging

Debugging for Vala programs can be done with either GDB or LLDB. For debugging in IDEs,

Examples

Hello world

A simple "Hello, World!" program in Vala:

voidmain(){print("Hello World\n");}

As can be noted, unlike C or C++, there are no header files in Vala. The linking to libraries is done by specifying --pkg parameters during compiling. Moreover, the GLib library is always linked and its namespace can be omitted (print is in fact GLib.print).

Object-oriented programming

Below is a more complex version which defines a subclass HelloWorld inheriting from the base class GLib.Object, aka the GObject class. It shows some of Vala's object-oriented features:

classHelloWorld:Object{privateuintyear=0;publicHelloWorld(){}publicHelloWorld.with_year(intyear){if(year>0)this.year=year;}publicvoidgreeting(){if(year==0)print("Hello World\n");else/* Strings prefixed with '@' are string templates. */print(@"Hello World, $(this.year)\n");}}voidmain(string[]args){varhelloworld=newHelloWorld.with_year(2021);helloworld.greeting();}

As in the case of GObject library, Vala does not support multiple inheritance, but a class in Vala can implement any number of interfaces, which may contain default implementations for their methods. Here is a piece of sample code to demonstrate a Vala interface with default implementation (sometimes referred to as a mixin)

usingGLib;interfacePrintable{publicabstractstringprint();publicvirtualstringpretty_print(){return"Please "+print();}}classNormalPrint:Object,Printable{stringprint(){return"don't forget about me";}}classOverridePrint:Object,Printable{stringprint(){return"Mind the gap";}publicoverridestringpretty_print(){return"Override";}}voidmain(string[]args){varnormal=newNormalPrint();varoverridden=newOverridePrint();print(normal.pretty_print());print(overridden.pretty_print());}

Signals and callbacks

Below is a basic example to show how to define a signal in a class that is not compact, which has a signal system built in by Vala through GLib. Then callback functions are registered to the signal of an instance of the class. The instance can emit the signal and each callback function (also referred to as handler) connected to the signal for the instance will get invoked in the order they were connected in:

classFoo{publicsignalvoidsome_event();// definition of the signalpublicvoidmethod(){some_event();// emitting the signal (callbacks get invoked)}}voidcallback_a(){stdout.printf("Callback A\n");}voidcallback_b(){stdout.printf("Callback B\n");}voidmain(){varfoo=newFoo();foo.some_event.connect(callback_a);// connecting the callback functionsfoo.some_event.connect(callback_b);foo.method();}

Threading

A new thread in Vala is a portion of code such as a function that is requested to be executed concurrently at runtime. The creation and synchronization of new threads are done by using the Thread class in GLib, which takes the function as a parameter when creating new threads, as shown in the following (very simplified) example:

intquestion(){// Some print operations for(vari=0;i<3;i++){print(".");Thread.usleep(800000);stdout.flush();}return42;}voidmain(){if(!Thread.supported()){stderr.printf("Cannot run without thread support.\n");return;}print("The Ultimate Question of Life, the Universe, and Everything");// Generic parameter is the type of return valuevarthread=newThread<int>("question",question);print(@" $(thread.join ())\n");}

Graphical user interface

Below is an example using GTK to create a GUI "Hello, World!" program (see also GTK hello world) in Vala:

usingGtk;intmain(string[]args){Gtk.init(refargs);varwindow=newWindow();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);varlabel=newLabel("Hello, World!");window.add(label);window.show_all();Gtk.main();return0;}

The statement Gtk.main () creates and starts a main loop listening for events, which are passed along via signals to the callback functions. As this example uses the GTK package, it needs an extra --pkg parameter (which invokes pkg-config in the C backend) to compile:

valac--pkggtk+-3.0hellogtk.vala 

See also

Related Research Articles

ScriptBasic is a scripting language variant of BASIC. The source of the interpreter is available as a C program under the LGPL license.

In programming languages, a closure, also lexical closure or function closure, is a technique for implementing lexically scoped name binding in a language with first-class functions. Operationally, a closure is a record storing a function together with an environment. The environment is a mapping associating each free variable of the function with the value or reference to which the name was bound when the closure was created. Unlike a plain function, a closure allows the function to access those captured variables through the closure's copies of their values or references, even when the function is invoked outside their scope.

<span class="mw-page-title-main">D (programming language)</span> Multi-paradigm system programming language

D, also known as dlang, is a multi-paradigm system programming language created by Walter Bright at Digital Mars and released in 2001. Andrei Alexandrescu joined the design and development effort in 2007. Though it originated as a re-engineering of C++, D is now a very different language drawing inspiration from other high-level programming languages, notably Java, Python, Ruby, C#, and Eiffel.

In computer science, reflective programming or reflection is the ability of a process to examine, introspect, and modify its own structure and behavior.

In computer programming, a callback or callback function is any reference to executable code that is passed as an argument to another piece of code; that code is expected to call back (execute) the callback function as part of its job. This execution may be immediate as in a synchronous callback, or it might happen at a later point in time as in an asynchronous callback. They are also called blocking and non-blocking.

<span class="mw-page-title-main">PyGTK</span> Set of Python wrappers for the GTK graphical user interface library

PyGTK is a set of Python wrappers for the GTK graphical user interface library. PyGTK is free software and licensed under the LGPL. It is analogous to PyQt/PySide and wxPython, the Python wrappers for Qt and wxWidgets, respectively. Its original author is GNOME developer James Henstridge. There are six people in the core development team, with various other people who have submitted patches and bug reports. PyGTK has been selected as the environment of choice for applications running on One Laptop Per Child systems.

<span class="mw-page-title-main">GObject</span> Free software library

The GLib Object System, or GObject, is a free software library providing a portable object system and transparent cross-language interoperability. GObject is designed for use both directly in C programs to provide object-oriented C-based APIs and through bindings to other languages to provide transparent cross-language interoperability, e.g. PyGObject.

<span class="mw-page-title-main">GLib</span> Software library

GLib is a bundle of three low-level system libraries written in C and developed mainly by GNOME. GLib's code was separated from GTK, so it can be used by software other than GNOME and has been developed in parallel ever since.

<span class="mw-page-title-main">C Sharp (programming language)</span> Programming language

C# is a general-purpose high-level programming language supporting multiple paradigms. C# encompasses static typing, strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines.

<span class="mw-page-title-main">Oxygene (programming language)</span> Object Pascal-based programming language

Oxygene is a programming language developed by RemObjects Software for Microsoft's Common Language Infrastructure, the Java Platform and Cocoa. Oxygene is based on Delphi's Object Pascal, but also has influences from C#, Eiffel, Java, F# and other languages.

Haxe is a high-level cross-platform programming language and compiler that can produce applications and source code for many different computing platforms from one code-base. It is free and open-source software, released under the MIT License. The compiler, written in OCaml, is released under the GNU General Public License (GPL) version 2.

Clutter is a discontinued GObject-based graphics library for creating hardware-accelerated user interfaces. Clutter is an OpenGL-based 'interactive canvas' library and does not contain any graphical control elements. It relies upon OpenGL (1.4+) or OpenGL ES for rendering,. It also supports media playback using GStreamer and 2D graphics rendering using Cairo.

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

<span class="mw-page-title-main">FutureBASIC</span>

FutureBasic is a free BASIC compiler for Apple Inc.'s Macintosh.

Genie is a modern, general-purpose high-level programming language in development since 2008. It was designed as an alternative, simpler and cleaner dialect for the Vala compiler, while preserving the same functionality of the Vala language. Genie uses the same compiler and libraries as Vala; the two can indeed be used alongside each other. The differences are only syntactic.

Seed is a JavaScript interpreter and a library of the GNOME project to create standalone applications in JavaScript. It uses the JavaScript engine JavaScriptCore of the WebKit project. It is possible to easily create modules in C.

Nemerle is a general-purpose, high-level, statically typed programming language designed for platforms using the Common Language Infrastructure (.NET/Mono). It offers functional, object-oriented, aspect-oriented, reflective and imperative features. It has a simple C#-like syntax and a powerful metaprogramming system.

C++/CX(C++ component extensions) is a language projection for Microsoft's Windows Runtime platform. It takes the form of a language extension for C++ compilers, and it enables C++ programmers to write programs that call Windows Runtime (WinRT) APIs. C++/CX is superseded by the C++/WinRT language projection, which is not an extension to the C++ language; rather, it's an entirely standard modern ISO C++17 header-file-based library.

Objective-C is a high-level general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXTSTEP operating system. Due to Apple macOS’s direct lineage from NeXTSTEP, Objective-C was the standard programming language used, supported, and promoted by Apple for developing macOS and iOS applications until the introduction of the Swift programming language in 2014.

<span class="mw-page-title-main">Nim (programming language)</span> Programming language

Nim is a general-purpose, multi-paradigm, statically typed, compiled high-level systems programming language, designed and developed by a team around Andreas Rumpf. Nim is designed to be "efficient, expressive, and elegant", supporting metaprogramming, functional, message passing, procedural, and object-oriented programming styles by providing several features such as compile time code generation, algebraic data types, a foreign function interface (FFI) with C, C++, Objective-C, and JavaScript, and supporting compiling to those same languages as intermediate representations.

References

  1. "Vala: high-level programming with less fat". Ars Technica. 2 September 2007. Retrieved 13 December 2011.
  2. "A look at two new languages: Vala and Clojure".
  3. "Vala· GitLab". GNOME . Retrieved 16 March 2021.
  4. Michael Lauer (2019). Introducing Vala Programming. doi:10.1007/978-1-4842-5380-9. ISBN   978-1-4842-5379-3. S2CID   207911698 . Retrieved 16 March 2021.
  5. "Vala Releases". Vala Project. Retrieved 18 March 2021.
  6. "Vala's Memory Management Explained".
  7. "Libgee on Gitlab".
  8. 1 2 "Coding in Vala with Visual Studio Code" . Retrieved 17 March 2021.
  9. "Coding in Vala with the Vim Text Editor" . Retrieved 17 March 2021.
  10. "Enable Vala syntax highlighting and code browser support in GNU Emacs" . Retrieved 17 March 2021.
  11. "benwaffle/vala-language-server on Github". GitHub . Retrieved 17 March 2021.
  12. "esodan/gvls on GitLab" . Retrieved 17 March 2021.
  13. "Vala Tools" . Retrieved 29 March 2021.
Comparison with other languages