Vala Tutorial

Author: gnome.org. Link to original: http://live.gnome.org/Vala/Tutorial (English).
Tags: c, c#, GNOME, Gtk, Gtk+, Linux, open source, Programming, Vala, программирование Submitted by space_indus 09.08.2010. Public material.
Vala is a programming language created with the goal of bringing modern language features to C, with no added runtime requirements and with little overhead, by targeting the GObject object system. It is being developed by Jürg Billeter and Raffaele Sandrini. The syntax borrows heavily from C#. Rather than being compiled directly to assembler or to another intermediate language, Vala is compiled to C which is then compiled with a platform's standard C compiler.

Translations of this material:

into Russian: Руководство по Vala. Translated in draft, editing and proof-reading required. Completed: 99%.
Submitted for translation by space_indus 09.08.2010

Text

Contents

1.Introduction

1.1.What is Vala?

1.2.Who is this tutorial for?

1.3.Conventions

2.A First Program

2.1.Compile and Run

3.Basics

3.1.Source Files and Compilation

3.2.Syntax Overview

3.3.Comments

3.4.Data Types

3.4.1.Value Types

3.4.2.Strings

3.4.3Arrays

3.4.4.Reference Types

3.4.5.Static Type Casting

3.4.6.Type Inference

3.5.Operators

3.6.Control Structures

3.7.Language Elements

3.7.1.Methods

3.7.2.Delegates

3.7.3.Anonymous Methods / Closures

3.7.4.Namespaces

3.7.5.Structs

3.7.6.Classes

3.7.7.Interfaces

3.8.Code Attributes

4.Object Oriented Programming

4.1.Basics

4.2.Construction

4.3.Destruction

4.4.Signals

4.5.Properties

4.6.Inheritance

4.7.Abstract Classes

4.8.Interfaces / Mixins

4.9.Polymorphism

4.10.Method Hiding

4.11.Run-Time Type Information

4.12.Dynamic Type Casting

4.13.Generics

4.14.GObject-Style Construction

5.Advanced Features

5.1.Assertions and Contract Programming

5.2.Error Handling

5.3.Parameter Directions

5.4.Collections

5.4.1.ArrayList<G>

5.4.2.HashMap<K,V>

5.4.3.HashSet<G>

5.4.4.Read-Only Views

5.5.Methods With Syntax Support

5.6.Multi-Threading

5.6.1.Threads in Vala

5.6.2.Resource Control

5.7.The Main Loop

5.8.Asynchronous Methods

5.8.1.Syntax and Example

5.8.2.Writing Your Own Async Methods

5.9.Weak References

5.10.Ownership

5.10.1.Unowned References

5.10.2.Ownership Transfer

5.11.Variable-Length Argument Lists

5.12.Pointers

5.13.Non-Object classes

5.14.D-Bus Integration

5.15.Profiles

6.Experimental Features

6.1.Chained Relational Expressions

6.2.Regular Expression Literals

6.3.Strict Non-Null Mode

7.Libraries

7.1.Using Libraries

7.2.Creating a Library

7.3.Example

7.4.Binding Libraries with VAPI Files

8.Tools

8.1.valac

8.2.vala-gen-introspect

8.3.vapigen

9.Techniques

9.1.Debugging

9.2.Using GLib

9.2.1.File Handling

Introduction

Disclaimer: Vala is an ongoing project, and its features may change. I will try to keep this tutorial as up to date as I can, but I'm not perfect. Also, I can't promise that the techniques which I suggest are necessarily the best in practice, but again I will try to keep up with that sort of thing.

What is Vala?

Vala is a new programming language that allows modern programming techniques to be used to write applications that run on the GNOME runtime libraries, particularly GLib and GObject. This platform has long provided a very complete programming environment, with such features as a dynamic type system and assisted memory management. Before Vala, the only ways to program for the platform were with the machine native C API, which exposes a lot of often unwanted detail, with a high level language that has an attendant virtual machine, such as Python or the Mono C# language, or alternatively, with C++ through a wrapper library.

Vala is different from all these other techniques, as it outputs C code which can be compiled to run with no extra library support beyond the GNOME platform. This has several consequences, but most importantly:

Programs written in Vala should have broadly similar performance to those written directly in C, whilst being easier and faster to write and maintain.

A Vala application can do nothing that a C equivalent cannot. Whilst Vala introduces a lot of language features that are not available in C, these are all mapped to C constructs, although they are often ones that are difficult or too time consuming to write directly.

As such, whilst Vala is a modern language with all of the features you would expect, it gains its power from an existing platform, and must in some ways comply with the rules set down by it.

Who is this tutorial for?

This tutorial will not go into depth about basic programming practices. It will only briefly explain the principles of object-oriented programming, instead focusing on how Vala applies the concepts. As such it will be helpful if you have experience of a variety of programming languages already, although in-depth knowledge of any particular one is not required.

Vala shares a lot of syntax with C#, but I will try to avoid describing features in terms of their similarity or differences with either C# or Java, with the aim of making the tutorial more accessible.

What will be useful is a reasonable understanding of C. Whilst this isn't needed for understanding Vala per se, it is important to realise that Vala programs are executed as C, and will often interact with C libraries. Knowledge of C will certainly make a deeper understanding of Vala far easier to come by.

Conventions

Pages: ← previous Ctrl next
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16