Sunday, September 14, 2014

Played around a little with the Vala programming language

Vala is a C# like programming language that compiles to simple C code using GObject and then obviously that gets compiled to assembly language with a C compiler. It's actually several years old but I must have passed over looking at it because I am a GC snob and Vala uses reference counting (like Objective-C and now Swift).

My first program was a tool to read from stdin and write to stdout removing duplicate lines.  Here's the code:

using Gee; // a collections library (written in Vala!)
int main() {
    var seen = new HashSet<string>();
    while (true) {
        string line = stdin.read_line();
        if (line == null) {
            break;
        }
        if (!seen.contains(line)) {
            seen.add(line);
            stdout.printf("%s\n", line);
        }
    }
    return 0;
}
(I'm probably missing a question mark in the declaration of line but it still built my binary (though I got some other warning from the underlying C compiler that can apparently be ignored). I'm formatting things a little more Java like than most of the Vala samples I've seen (they put a space between the method name and the parens around the arguments).

What's pretty cool is this compiles into a very small binary: 13,472 BYTES! I haven't done a speed test except for pretty small test files but there was virtually no "start up" costs, a complaint often made of Java (and possibly other VM systems like Mono).

Another advantage Vala should have is that although you still need a "vapi" it looks like Vala can call C code without a big impedance mismatch (which should make it high performance for certain tasks).

I wrote another utility to find numbers in each line and append a comment to each line that that would print out the corresponding date if those numbers are millis since the epoch. This was also small and fairly simple though I did run into a reference counting related quirk:

This won't work:
var builder = new StringBuilder().append("xyz");
Because of reference counting and owned versus unowned type system stuff, this needed to be:
var builder = new StringBuilder();builder.append("xyz");
(But then method chaining should work fine).

Working my way through more little utilities, a place I suspect Vala could really shine, I hit what is probably a documentation versus version issue trying to create threads. I'm using Linux Mint 17 and don't appear to have the latest glib or something. I figured maybe I could get stuff working with an IDE to tell me what methods are really there, not what the Vala documentation says so I grabbed monodevelop which again, versioning is not helping me here. I got version 4.0 in Linux Mint 17 but that doesn't support Vala yet. So, then I tried to download monodevelop from git and build it myself. I bet you can't guess what happened when I tried to run config? You guessed it, another versioning problem, this time glib-sharp needs to be at least 2.12 but I guess I only have 2.0 (though I see a 3.0 version available and I installed that but autoconf doesn't seem to be picking it up).

OK, I really don't do much Linux development (my day job is hacking Java code). Maybe this is kind of normal for linux development?

Vala isn't at version 1.0 yet (just .25 released on Sept 1, 2014) but since apparently Elementary OS uses it, I was hoping to give it a more serious look. Some non trivial apps have been written in it.

Summary

Naturally, Vala needs a stable 1.0 release that is included in popular distros, the documentation is not terrific right now and the website is ugly. There is some tutorial type documentation but they don't have many advanced examples to learn from yet. And of course, they need a great language specification and those are pretty difficult to create even if the compiler itself and the concepts are sound. These are real criticisms but they are definitely ones that can be solved especially if Vala's popularity grows and more people become involved.

I really like where Vala seems to be headed and look forward to getting an environment set up so I can play with it more. I wouldn't bother writing this blog post if I thought Vala is a waste of time or just another stupid language. It honestly seems like Vala could be a great practical niche language that could grow in popularity. My brain has been warped by Java but the ability to write some "C code" in a Java-ish way is appealing to me.

I'll probably look a little more at mono and C# under linux too. If you're writing a sufficiently complex program, run time environments like the JVM or Mono aren't really that big a deal.



Saturday, August 30, 2014

Complex prompts in bash

I've been using a a bash script to generate my prompts for quite a while though I didn't realize that it wasn't working in normal shells (anything besides Emacs).

The flawed setup is as follows:

.bashrc
export PROMPT_COMMAND="my_custom_bash_prompt_script"
export PS1=""

This setup works fine from within an Emacs shell but from a "normal" shell, when I did an up arrow, the prompt itself would disappear!

The corrected setup is very similar:

.bashrc
function bash_command_function {
  PS1=`my_custom_bash_prompt_script`
}
export PROMPT_COMMAND="bash_prompt_script"

Once I realized that I needed a bash function anyways (because you can't export something from a subshell to the super shell), I just inlined my rather simple script.

Keep in mind, if my_custom_bash_prompt_script ever returns anything with a % character in it, bash may interpret it, so you will need to escape the %s.