Well, you could say it is, but actually it’s a programming language. But nobody heard of it so there’s nothing awesome about it, you might say. WRONG! Here are some of its characteristics that caught my attention:

  • performance on the same level as C, but it’s more of a high-level language
  • compiles Vala code and outputs C code ≡ C cannot do X ⇒ Vala cannot do X
  • syntax ≈ C# (or Java)
  • valac is the name of the compiler; it’s used exactly like gcc
  • some details about data:
    • constants are defined as: const UPPER_CASE
    • you can get the MAX or MIN of a type by using: data_type.MIN or data_type.MAX (int.MIN etc.)
    • strings
      • UTF-8
      • immutable (Java-like)
      • templates: if a = 6 and b = 7 then “$(a*b)” will be evaluated to “42”
      • can be sliced: some_string[start:end]
      • methods like parse or to_string are available
      • in can be used to find a string in another string (instead of strstr in C)
  • delegates: passing functions/methods as objects/variables
  • there is no overloading!
    • for overloading constructors you need to use named constructors:
      • new Button();
      • new Button.with_label(“Click me”);
      • new Button.from_stock(Gtk.STOCK_OK);
    • constructors can be chained by using this
  • signals = events
    • require connect (handlers)
    • are better used with lambda functions (closures)
    • are activated by extern factors
    • always belong to instances of classes
    • every instance of a class derived from GLib.Object has a signal called notify which gets emitted every time a property of its object changes! (properties, later on)
    • examples:
      • instance.signal.connect((t, param_of_signal) => {…});
      • obj.notify.connect((s, p) => { stdout.printf(“Property ‘%s’ has changed! \n”, p.name);
        • s = source of the signal
  • properties
    • alice.age++; instead of alice.set_age(alice.get_age() + 1);
    • public int age {get; set; default = 32; }
      • or private set/get, or no set/get at all
      • C#-like
    • keyword construct can be used alongside set/get and whatever is implemented in construct { … } is called after the constructor is called. (starting from the first superclass every class calls its construct block if it exists)
  • contract programming
    • double method_name(int x, double d)
    • requires (x > 0 && x < 10)
    • requires (d >= 0.0 && d <= 1.0)
    • ensures (result >= 0.0 && result <= 10.0)
    • {
      • return d * x;
    • }
    • result = special variable = return value
  • pointers
    • exactly like C
  • chained relational expressions
    • if (0 < a && a < b && b < c) { … }
  • regex allowed!
  • OOP
    • base = super
    • every abstract method of an abstract class must be overridden
    • virtual can be overridden but it is not necessary
    • when implementing interfaces methods should have the same name
    • when implementing multiple interfaces:
      • interface Foo { public abstract int m(); }
      • interface Bar { public abstract string m(); }
      • class Cls : Foo, Bar {
        • public int Foo.m() { return 10; }
        • public string Bar.m() { return “bar”; }
      • }
    • keyword as:
      • (Button) widget ≡ widget as Button
      • less parentheses
    • keyword is:
      • boolean function that tells us if a variable is of a certain type
      • Button b = (widget is Button) ? widget as Button : null;
  • GLib.Object = The Mother of All Classes
    • namespaces are available so using GLib; will allow us to use Object alone

These are just some things I found interesting about Vala. More of it can be found at Valadoc.

As the sixth season of GoT is almost over, all I can say is “Vala(r) morghulis”.


5 thoughts on “Vala? Is it tasty?

  1. how come every article about Vala is always missing best feature there is.

    you can call mix C and Vala without resorting to any library. you just include .c ..h files and supply .vapi so you can call them from code in Vala. and with just a little knowledge in how code is processed, you can call Vala from C by using vala generated headers

    this is especially useful when you don’t have bindings and making ones wouldn’t be worth the trouble

    Liked by 1 person

      1. another thing you might consider is interfaces. Foo and Bar could as well implement their own methods since they don’t refer to anything object specific

        and even when they do you can simply abstract those so they get processed correctly

        public interface Foo
        public abstract string get_some_val();
        public void writeout()
        stdout.printf (“%s\n”, get_some_val());

        at this point every class that implements Foo needs to supply get_some_val, writeout() is already implemented by default.


  2. “compiles Vala code and outputs C code ≡ C cannot do X ⇒ Vala cannot do X”

    But there is no X since you can do anything in C. 🙂 Vala just reduces LOC.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s