编程语言之Vala
2014-04-11 20:45:47 阿炯

Vala是专为Gnome环境准备的开发语言,之所以vala会出现,是因为目前的glib、gio、gtk+、gnome-lib等等的c语言接口开发起来实在太繁琐,而同时如果采用java,c++这些东西,那又显得太臃肿。Vala采用C语言开发并在LGPL协议下授权,是一门面向对象的编程语言;其目标是为GNOME开发者提供现代化的编程语言功能,同时没有额外的运行时要求,并且相对于c语言编写的应用程序和库,无需使用不同的ABI。它基于GNOME的GObject系统,提供了那种你期望的类功能。由自主编译器产生C语言代码和使用GObject系统,允许在Gnome运行时库的基础上使用一些现代的编程技巧,通过使用GLib和GObject,Vala提供了动态类型系统和自助内存管理的功能。

与Scala一样,在语言级别上,它具有与Java Beans的get(*)和set(*)等同的属性,但对于属性更改通知和监听器(listener),它也能够提供了语言级别上的支持。它不具备很强的XML处理能力,只是一个迷你型的解析器,不过看起来它的确具有很好的Unicode功能(字符串为UTF-8,string.getch()可返回16位的unichar字符)。在功能级别上,Vala与Java最大的不同之处应该是对象回收(de-allocation)的引用计数系统(reference-counting),相对于Java的GC,这一系统能够提供更多确定的实时行为,不过也为某些编程错误打开了一扇窗户。Vala的另一个特点在于,对于阵列访问不进行边界检查,所以它比Java对规则的要求更为严格但比C语言要宽松。


Vala is a new programming language that 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.

vala是对c语言的一种高层封装,集成并实现了gnome环境的函数,在编译的时候首先vala代码会转成c代码,然后再编译。这么做就相当于用简单的方式来编写c程序,而且又有面向对象的特性,同时又不失c语言的优秀特性。Vala使用类似C#的语法,编译过程是先转换成C源码,然后使用标准的C编译器,生成二进制执行文件,这样编译出的文件,执行速度很快。同时,这样的编译过程对系统的编译环境要求很低,只要你的系统自带了C编译环境。Vala标准的文档不多,但基本在 valadoc.org 均可很方便的搜索到。遇到编程问题,可以加入到官方的邮件列表提问。

推荐尝试使用 Vala 的十大理由:

1、语法兼容C#和Java,表达能力强,生产力高
语法干净、简洁、表达能力强,没有让人晕菜的 ->/&/:,点号(.)搞定一切。

2、内存所有权和使用引用计数管理,内存使用安全无烦恼。

3、支持接口:支持抽象接口,也支持接口实现
接口实现类似于PHP traits 和 JavaScript 的mixin的功能。

4、命名字空间支持大型应用开发

5、语言级别的正则表达式支持

6、有C的速度和良好的互操作性支持
可以生成C头文件和C代码并获得全平台支持,也可以Vala与C混合编译。

7、强大且成熟的类库支持(官方支持约250个库)
基于GLib的强大类库体系,有工业级成熟度的各种库和GUI框架。默认基于 GObject 的对象系统,也支持构建自己的单根对象体系。

8、有丰富且完善的文档支持(valadoc.org)也有大量开源项目可供参考学习

9、有多种语言服务器[LSP]实现,支持各种编辑器和IDE
https://gitlab.gnome.org/esodan/gvls
https://github.com/benwaffle/vala-language-server

10、Gnome 和 Elementary OS官方开发语言,并获得IBM公司研究机构采用。


一个简单的“Hello world”程序:
void main () {
    print ("Hello World\n");
}

更完整的一个版本,表现了Vala的一些面向对象的特性:
class Sample : Object {
    void run () {
        stdout.printf ("Hello World\n");
    }

    static void main (string[] args) {
        var sample = new Sample ();
        sample.run ();
    }
}


如何在Windows平台安装Vala编译环境

ValaWindowsInstaller

Vala programming language distribution for Windows.Windows all-in-one distribution of the Vala programming language to build console and GTK+ GUI programs. Includes Vala command-line compiler, MinGW C compiler toolchain and GTK+ version 2 and 3 runtimes.


Welcome to the Windows all-in-one distribution of the Vala programming language. This package contains everything one needs to create Vala-written console and GUI programs on Windows operating system.With this distribution building the basic Vala program on Windows is as easy as

> valac hello.vala

Even building a full-fledged GTK+ program on Windows is equally a one-liner

> valac --pkg gtk+-3.0 -X -mwindows gtk.vala

The above sample Vala sources can be found in the <Vala for Windows>\share\example directory.

The distrubution comes in 32-bit and 64-bit flavors and bundles the Vala compiler itself, the MinGW GCC compiler toolchain and custom builds of the GLib and GTK+ stacks.

The latest distribution ships the following versions of software:
    Vala - 0.36.4
    Libgee - 0.20.0
    GCC - 5.4.0
    GLib - 2.52.3
    GTK+2 - 2.24.31
    GTK+3 - 3.22.16

The distribution download page is here(https://sourceforge.net/projects/valainstaller/files/).

Notes

The shipped Vala compiler and assotiated tools are patched to make automatic use of the bundled GCC toolchain and GLib/GTK+ libraries therefore it is not required to have them in PATH in order to build executables. This is made to prevent pollution of the PATH environment variable with GCC and GTK+ binaries which might interfere with other installations. The decision whether or not to augment the PATH is made at the distribution install time. A consequence of omitting the PATH augmentation is that the produced binaries will refuse to run due to missing DLLs. In this case the required libraries along with its dependencies are to be manually copied from the <Vala for Windows>\local\bin directory.


另一个在Windows下的安装源(爱好者提供)

Compiling Vala on Windows

再来一个

Vala on Windows(官方说明,借助了msys2这个在windows下移植过来的linux运行环境)


Example

Install msys2 (http://www.msys2.org/)

Install the required prerequisites for Vala
pacman -S mingw64/mingw-w64-x86_64-gcc
pacman -S mingw64/mingw-w64-x86_64-pkg-config
pacman -S mingw64/mingw-w64-x86_64-vala

and all the additional packages your code requires, i.e.

pacman -S mingw64/mingw-w64-x86_64-libgee
...

Launch the correct msys2 shell

C:\msys64\mingw64.exe

Check the MSYSTEM and PKG_CONFIG_PATH environment variables

$ echo $MSYSTEM
MINGW64

$ echo $PKG_CONFIG_PATH
/mingw64/lib/pkgconfig:/mingw64/share/pkgconfig

Run valac as usual, but make sure to always work in the correct environment (see steps 3 and 4)

For example let's build the first GeeSample here:

$ valac gee-list.vala --pkg gee-0.8

笔者试用说明:在win7 32bit系统下,需要将c:\msys32目录下的各个bin加入到PATH变量中,在有Gtk模块进行最简单编译时有报警(使用gtk+-3.0时报gtk.h找不到),只能用gtk2,但能生成最终程序且能运行。所以还是推荐用sourceforge上的valainstaller包。


开始之旅

1、安装编译环境
apt-get install valac

2、编写hello world
vim a.vala
using GLib;
public class HelloVala: GLib.Object{
 public static int main (string[] args){
  stdout.printf ("Hello FreeOA!\n");
  return 0;
 }
}


3、编译
valac -o a a.vala

4、运行即可

可以看到 vala的编译器是valac。这是一个没有界面的只在cmd模式下运行的程序,下面来一个有界面的程序:freeoa.vala
using Gtk;

int main (string[] args) {
    Gtk.init (ref args);
    var window = new Window ();
    window.title = "我的 GTK+ Program";
    window.border_width = 10;
    window.window_position = WindowPosition.CENTER;
    window.set_default_size (800, 640);
    window.destroy.connect (Gtk.main_quit);

    var button = new Button.with_label ("点我必达");
    button.clicked.connect (() => {
        button.label = "Thank you";
    });

    window.add (button);
    window.show_all ();

    Gtk.main ();
    return 0;
}


编译:valac --pkg gtk+-3.0 -X -mwindows freeoa.vala

完成后会在同级目录下生成freeoa的二进制文件,点击后执行。


最新版本:0.46
自 Vala 0.44 以来的主要更新如下:
1、依赖 GLib 2.48 以上版本
2、增加目标依赖库选项 AUTO(--target-glib=auto)
3、异常类(error-domains)支持支持静态方法
4、Bug修复
5、各种库更新

详细的更新说明请参考这里

最新版本:0.47
自 Vala 0.46 以来的主要更新如下:
1. POSIX 配置的若干改进
2. 代码生成的改进
代理(Delegate)支持传递和查询可变参数
对象支持NULL结尾的无长度数组
对象支持代理(Delegate)属性
直接访问强制转换表达式的可访问成员告警
3. Vala 编译器的改进项
增加 DelegateType.target/destroy 以支持访问其用户数据
接受接口类型为对象类型的内部符号类型
增加 GenericType.dup/destroy 字段
支持生命局部变量为 unowned var 类型
4. 代码生成修复了6个Bug
5. 编译器修复了6个Bug
6. 文档修复 3个Bug
7. 支持库绑定有 8 处更新


官方主页:https://vala.dev/

项目主页:https://wiki.gnome.org/Projects/Vala