A Neophyte's Journey to Vala

This is a recounting of one person's journey to learning Vala. Because I am a relatively inexperienced programmer with knowlege of some scripting languages, the recounting, which is meant as a beginner's tutorial, will be for people like me. Contributions and corrections from others are encouraged. All errors are my own. (By the way, if you catch any of my errors, please write me: aigiskos AT yahoo DOT com . Thanks!)

  • By the way, there is an excellent tutorial for Vala out there which caters to a more sophisticated readership (read: not as ignorant and foolish as I). You will find it here: http://live.gnome.org/Vala/Tutorial .

Why Vala?

People far more eloquent and knowledgeable than me have answered that question in the tutorial referenced above: http://live.gnome.org/Vala/Tutorial .

How to get Vala

A tarball of the source code is located is located at http://live.gnome.org/Vala . If you download the tarball, an archive manager may pop up to extract the files for you. Alternatively, you can extract the files on the command line with the tar -x command. Once you have extracted the files, however, you must compile them with a compiler such as gcc. I confess that I have not managed to get this to work because other files are needed to compile the downloaded and extracted Vala files. Someone who knows exactly which other files are needed, where to obtain them, and where to put them so that gcc will work is highly encouraged to add that information here!

I happen to be using Ubuntu, which is part of the Debian family of Linux distributions. The Debians use apt-get for package management, and Ubuntu is no exception. Some very helpful people on irc told me to use apt-get -build-dep vala, and, by George, it worked! (Thanks, guys!)

People using rpm or other type systems are highly encouraged to add here how to get Vala on their systems.

Now What?

Let's start with the first program, which I will call Arthur after the hapless Arthur Dent in the Hitchhiker's Guide to the Galaxy (which I highly recommend you buy from your nearest bookstore).

Open up your favorite text editor. (Not word processor, mind you: using AbiWord or OpenOffice will cause you problems. If you are using Gnome, gedit should do the trick.)

First Program

//First program.  The excitment is killing me!!!
//That was a comment, by the way.  So is this, come to think of it.

using GLib;

public class Arthur : Object {
     static void main () {
          Arthur object = new Arthur ();
          stdout.printf ("DON'T PANIC.\n");
     }
}

Now save the file that holds that marvelous program some place where you can find it again. I'll call it Arthur.vala . (The .vala is important, by the way. Your files will always need the .vala part at the end of their name or the compiler will get confused.)

Compiling

Once you have done that, go to that unforgettable place where you just saved the file in a terminal. Then type in the following:

valac -o Arthur Arthur.vala

If everything goes ok (and I hope it does!), you'll see this message: Compilation succeeded - 0 warning(s) . Congratulations! You have programmed (and compiled!) your first Vala program.

Phew. That wasn't so bad! This is a program designed to do just one thing: print out the sentence DON'T PANIC. in a terminal for your viewing pleasure.

Running the Program

To make sure it worked, type the following in the command line:

./Arthur

(A little explanation: the file you type your beautiful code into is known as source code . This means it is (at least theoretically) readable to humans. Source code files in the Vala programming language end with the suffix .vala. Valac is the compiler which translates your source code into a new file that is machine readable. That machine-readable file will have the same name as your source code file minus the .vala suffix. So, here Arthur.vala is the source code file,and Arthur is the machine-readable file that actually does something the computer will respond to. So, to run the program, you must type in Arthur on the command line. The ./ tells your command shell to look for the program in the current working directory.)

Taking the First Program Line by Line

//First Program.  The excitement is killing me!!! 

This is a comment which is some text which is completely ignored by the program that compiles your program into the bunch of ones and zeros your computer will understand. In other words, a comment serves no purpose so far as your computer is concerned. Don't let that fool you: comments are very important. They help you, dear programmer, remember what you were doing when you wrote that bit of brilliant code, so that it doesn't look like drug-addled jibberish when look at it weeks or months later. Comment early. Comment often. Comments are your friends.

Note: comments such as this one begin with  //  . They only last one line. Once you push Enter, your comment is done, and the compiler begins to pay attention to what you're writing again.

using GLib; 

There are times when you want to use code that someone has already written and debugged. The using keyword tells the compiler valac that you will be using code falling under the name of GLib (In computer-speak, they call the whole falling under a name thing a namespace.). Even more specifically, using GLib; tells valac that you will be using that code but don't want to keep telling valac that the code is in the namespace of GLib.

It is rather like you are having an extended conversation about a guy named Ford Prefect. It would be pretty tiresome to say, "Ford Prefect" everytime you're referring to the guy. You would probably just say "Ford" instead, unless there was some special need for you to specify which Ford you're talking about.

As there are potentially a lot of Prefects out there in the Prefect family (at least theoretically) and only one Ford Prefect (we'll hope), we say that Ford falls under the namespace of Prefect.

It's the same thing here. GLib is the namespace. We're just telling valac that we will be referring to things in the GLib family without actually saying that they are part of the GLib family.

public class Arthur : Object { 

Vala, like Java and C#, loves things called classes. Classes are blueprints that tell the compiler how to churn out actual working pieces of code to the class's specifications. These working pieces of code are called objects or sometimes instances . (Please excuse all the exciting computer terminology. It can be too stimulating at times, I know.)

Vala will force you to define at least one class to make your program work. In our case, we proudly made a class called Arthur.

The : Object simply tells valac that our class is related to a class in, you guessed it!, GLib called, somewhat confusingly, "Object." Don't spend too much thought on this: all of your classes will be decended from Object for now.

All classes are either public or private. Don't worry about it: mark your classes public for now.

This leads us to the  {  at the end of the line. This is a bracket . It will have a mate that looks like  }  somewhere farther down in your program. In this case, the bracket tells the compiler that everything from itself to its  }  mate (which is on line 8) is part of the class Arthur. By convention, you should indent the lines between the brackets to help differentiate them from other parts of your program.

As I come from a python background, I have to force myself to use brackets (and semicolons, which we'll get to momentarily), but brackets are rather useful.

static void main () {

This is our first sighting of a method. If you look at computer programming from a distance (and maybe squint a bit), you can say that programs generally consist of data and things that create or manipulate that data. Methods are the primary creative (and manipulative!) tools for programming: they do stuff to data. If you compare programming languages to human languages, you could say that methods are the verbs and your data are the nouns.

Depending on how you define a method, you can feed the method information (which it can manipulate!), and the method can spit back to you some information. In this case, our method definition is very simple: we neither give it information or ask it to give anything back.

main is the name of the method. void simply tells us that main does not spit back any information to us. The () tell us what we would feed the method. In this case, there is nothing in the parentheses, so, by definition, we never feed information to the method. Don't worry about static for right now.

You should know that main is an extra special method: every Vala program must have a main method in one of its classes to run. The compiler will look for main and run it first. Also main is always static. (Again, we'll get to that later.)

Arthur object = new Arthur ();

Remember above where we talked about working pieces of code called objects? Well, this is how you create an object from a class. Arthur object tells the compiler that you plan to have an object, which we appropriately, if unimaginatively, are calling "object".

= The equal sign is an assignment operator. All that means is that you are telling the compiler that whatever is on the left of the equal sign is being given the value on the right side of the equal sign. So, in this case, you are telling the compiler that your thing called "object" which should be an object of the class of Arthur is being assigned to the expression new Arthur ().

new Arthur () is the way you create an object (in this case from the class Arthur). new tells the compiler that you are creating an object, and Arthur () tells it you are creating an object from the class of Arthur. Every class has a method with the same name as the class. To create a new object of that class, you type new and then that method. Note: generally, when you "call" a method, you must type parentheses after the method name. If you are not feeding any information to the method, the parentheses will be empty, just as they are here.

That leads us to the semicolon ;. A semicolon ends a thought in programming, much like a period does in English. It tells the compiler that whatever is written on the next line is not stricly part of what is written on the present line.

Note: since Vala forces you to create a class for every program, you must always create an object of that mandatory class for your program to run.

Ah, now the meat:

stdout.printf ("DON'T PANIC.\n")

'printf' is a method found in GLib and attached to something called stdout. Don't worry about stdout for now. Just know that to print something to the console, you need to type stdout.printf and feed it a string, demarcated by quotation marks "". Like all things you feed methods, the food should be within parentheses.

\n, by the way, simply tells the terminal to start the next piece of text on a new line, as if you pushed the Enter key.

That was a lot to take in. Don't sweat it. We'll cover each of the above concepts in more detail below.

Variables: How to Represent Your Data to Vala

In any programming language, you need to work with data and usually give that data names so that your program can keep track of that data. Vala stores data values in variables which are basically containers that hold the data and have names attached to them.

Vala is what computer people call a statically typed language. That's a fancy term which means that you must tell the language what kind of data you plan on putting in each variable when you first declare the variable. It also means that you can't try to put another kind of data in that variable later. This aspect of Vala (which is shared by Java and C#) is more restricting than some other languages, but allows the compiler to catch all sorts of errors that more permissive languages like python or ruby will only trip upon when the user is using your program.

How to declare a variable

This is easy. You simply tell the compiler the type of data you will put in the variable followed by the variable name. For instance, if you want to store an integer number into a variable entitled theMeaningOfLifeTheUniverseAndEverything, you would simply write:

int theMeaningOfLifeTheUniverseAndEverything;

You could also assign a value to that variable at the same time as you declare it.

int theMeaningOfLifeTheUniverseAndEverything = 42;

Naming a Variable

Variables may start with an underscore (i.e. _) or with an letter (a through z). After that, the variable may have letters, numbers, or underscores in them. The letters can be upper case (e.g. A) or lower case (e.g. a). Be careful: Vala is case sensitive, so the variables life, Life, and LIFE are all different in Vala's eyes.

In addition, you should know that there are certain key words which Vala has set aside for itself and which cannot be used as a variable name. These are the following:

  • abstract
  • as
  • base
  • bool
  • break
  • byte
  • case
  • catch
  • char
  • checked
  • class
  • const
  • construct
  • continue
  • decimal
  • default
  • delegate
  • do
  • double
  • else
  • enum
  • event
  • explicit
  • extern
  • false
  • finally
  • fixed
  • float
  • for
  • foreach
  • goto
  • if
  • implicit
  • int
  • interface
  • internal
  • is
  • lock
  • long
  • namespace
  • new
  • operator
  • out
  • override
  • params
  • private
  • protected
  • public
  • readonly
  • ref
  • return
  • sbyte
  • sealed
  • short
  • sizeof
  • stackalloc
  • static
  • string
  • struct
  • this
  • throw
  • true
  • try
  • typeof
  • uint
  • ulong
  • unchecked
  • unsafe
  • ushort
  • using
  • virtual
  • volatile
  • void
  • while

Here are some examples of good and bad variable names (bad ones will result in an error):

  • Good:
    • zaphod
    • Zaphod
    • _Zaphod
    • _Zaph0d
    • zaphod32
    • zaph33d
    • zaphod_has_two_heads
    • presidentOfTheGalaxy
    • president_of_the_galaxy
    • PresidentOfTheGalaxy

  • Bad:
    • $hello
    • @greetings
    • gutenTag!
    • hello?
    • 99bottles
    • new
    • static
    • h@lo
    • @angel

Data Types

Projects/Vala/NeophyteTutorial (last edited 2013-11-23 01:23:23 by WilliamJonMcCann)