2008-11-03
Recently, I decided to start learning the Vala programming language. As with any programming language, I find that the easiest way to learn is to create a real world program, and I don't mean "hello world". During the start of my Vala development, I became aware of a few issues that took me a while to hunt down solutions for and I thought it would be best to share the info I have collected. Vala, as of this writing, is at version 0.4 and being quite new, the language doesn't have the most in-depth documentation, so I'll use working code examples.

Issue 1: How do I convert an integer into a string in Vala?
Answer: Vala strings are the same as GLib strings - Gstrings, yea, laugh it up. As such one can use the "printf" function of the string object to change an integer to a string. Take the following code as an example:
//nearly all vala apps will use Glib
using GLib;

//create a class that will we will call to print a string
public class Printer {
//create a function that we can use to print a string
public void print_line(string str)
{
stdout.printf("%sn",str);
}
}

//create the main class
private class Main {
//create the entrance to the application
private static void main()
{
//make an instance of the printer
var printer = new Printer();
printer.print_line("hello world");
//loop 5 times
for(int i=0; i<5; i++)
{
// convert the integer value of i to a string
string i_string = "%d".printf(i);
//print the value of i
printer.print_line("i = "+i_string);
}
}
}


OK, did you copy the code and paste into your favorite editor?
Let's look at line 25:
string i_string = "%d".printf(i);

I created a string object named 'i_string' and set it equal to a string object that is print formatting an integer.

Issue #2: How do I list the actual name of the package that I can include when I'm compiling with valac?
Answer: When compile a Vala app that uses libraries like GTK, XML or Webkit, one needs to add '--pkg [packagename]' to the compile arguments used by valac. A list of the available packages can be found by doing the following:
1. on the commandline enter 'which valac' to find the install location of the valac compiler, since my compiler is installed in /usr/local/bin.....
2. on the commandline, I enter 'ls /usr/local/share/vala/vapi' and I will see a list of all of the packages I can use.
Had my valac been installed in /usr/bin, I would search for packages in /usr/share/vala/vapi

Issue #3: OK, this isn't really an issue, but it was a oddity that I had to accept. All Vala apps have an entry point in a function named "main", that is to say, that Vala code starts execution at a function named "main" and this function can create an instance of the class that "main" is a function of. To me this seemed very weird. Oh well, I got over it. Re-writing the above code to show the main function creating an instance of the class that main is a function of results in the following code:
//nearly all vala apps will use Glib
using GLib;
//create the printer class
private class Printer {
//create a function that we can use to print a string
public void print_line(string str)
{
stdout.printf("%sn",str);
}
//create the entrance to the application
private static void main()
{
//make an instance of the printer
var printer = new Printer();
printer.print_line("hello world");
//loop 5 times
for(int i=0; i<5; i++)
{
// convert the integer value of i to a string
string i_string = "%d".printf(i);
//print the value of i
printer.print_line("i = "+i_string);
}
}
}


By the way, I've been happily writing all of my Vala code using valide: a Vala specific IDE.
Comments
2008-11-11 jezra:
Oh geez. I could have used the "to_int()" method of the string class. Example:
string number="1";
int value = number.to_int();
2008-12-29 Justin:
I think you mean that you could have used to_string().

For example, instead of

string i_string = "%d".printf(i);

you could have written

string i_string = i.to_string();

Thank you very much for the post though. Vala has caught my interest.

Justin
2008-12-29 jezra:
oops,
Yes, I should/could have used 'to_string()' to convert the int to a string.
Thanks for the catch.
2009-08-18 Arkadi:
Thank you very much for your post.
I'm learning VALA too and writing a blog my self... Good luck.
Name:
not required
Email:
not required (will not be displayed)
Website:
not required (will link your name to your site)
Comment:
required
Please do not post HTML code or bbcode unless you want it to show up as code in your post. (or if you are a blog spammer, in which case, you probably aren't reading this anyway).
Prove you are human by solving a math problem! I'm sorry, but due to an increase of blog spam, I've had to implement a CAPTCHA.
Problem:
2 plus 8
Answer:
required
subscribe
 
2019
2016
2015
2014
2013
2012
2011
2010
December
November
October
September
August
July
June
May
April
March
February
January
2009
December
November
October
September
August
July
June
May
April
March
February
January
2008