Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refacting the vala doc #50

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
53 changes: 33 additions & 20 deletions source/tutorials/programming-language/main/01-00-first-program.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,62 @@ Sadly predictable, but still:

.. code-block:: vala

class Demo.HelloWorld : GLib.Object {
public static int main(string[] args) {
stdout.printf("Hello, World\n");
return 0;
}
}
void main() {
stdout.printf("Hello, World\n");
}

Of course, that is a Vala *Hello World* program. I expect you can recognise some parts of it well enough, but just to be thorough I shall go through it step by step.

.. code-block:: vala

class Demo.HelloWorld : GLib.Object {
void main() {


This line identifies the beginning of a class definition. Classes in Vala are very similar in concept to other languages. A class is basically a type of object, of which instances can be created, all having the same properties. The implementation of classed types is taken care of by the *gobject* library, but details of this are not important for general usage.
This is the start of a method definition. A method is a function related to a type of object that can be executed on an object of that type. The fact that this method is called ``main`` and has the signature it does means that Vala will recognise it as the entry point for the program. ``void`` is the return type of the method, which means that the method does not return any value.

What is important to note is that this class is specifically described as being a subclass of *GLib.Object*. This is because Vala allows other types of class, but in most cases, this is the sort that you want. In fact, some language features of Vala are only allowed if your class is descended from GLib's *Object*.

Other parts of this line show namespacing and fully qualified names, although these will be explained later.

.. code-block:: vala

public static int main(string[] args) {
stdout.printf("Hello, World\n");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for beginners I think just print() is better. Then there can be a note explaining that in actual projects people prefer stdout.printf() and why, maybe in the section also where the different main() variants are explained

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine with me, I agree, to show the world that à la is easy to write! 😎



This line instructs Vala to execute the method called *printf* of the *stdout* object, with the hello string as an argument. In Vala, this is always the syntax you use to call a method on an object, or to access an object's data. ``\n`` is the escape sequence for a new line.

This is the start of a method definition. A method is a function related to a type of object that can be executed on an object of that type. The static method means that the method can be called without possessing a particular instance of the type. The fact that this method is called ``main`` and has the signature it does means that Vala will recognise it as the entry point for the program.

The *main* method doesn't have to be defined inside a class. However, if it is defined inside a class it must be ``static``. It doesn't matter if it's ``public`` or ``private``. The return type may be either ``int`` or ``void``. With a ``void`` return type the program will implicitly terminate with exit code 0. The string array parameter holding the command line arguments is optional.
---------------

there are several ways to write a ``main`` method in vala, here are the possible ones :

.. code-block:: vala

void main () {
}

stdout.printf("Hello, World\n");
int main () {
return 0;
}

int main (string[] args) {
return 0;
}

*stdout* is an object in the *GLib* namespace that Vala ensures you have access to whenever required. This line instructs Vala to execute the method called *printf* of the *stdout* object, with the hello string as an argument. In Vala, this is always the syntax you use to call a method on an object, or to access an object's data. ``\n`` is the escape sequence for a new line.
void main (string[] args) {
}

.. code-block:: vala
it is possible to declare your main in a class only if it is public and static.
``int`` is the return type of the main method, which means that the method returns an integer value. The integer value returned by the main method is the exit status of the program. The exit status is a value returned by a program to the operating system. The operating system can then use this value to determine whether the program executed successfully or not. A return value of 0 indicates that the program executed successfully, while a non-zero value indicates that the program did not execute successfully.

``string[] args`` is an array of strings that are passed to the program when it is executed. The first element of the array is the name of the program itself. The remaining elements are any arguments that are passed to the program when it is executed. The arguments are separated by spaces. For example, if you execute the program with the command ``./hello arg1 arg2``, then the array will contain the following elements:

return 0;

``return`` is to return a value to the caller and terminate the execution of the *main* method which also terminates the execution of the program. The returned value of the *main* method is then taken as the exit code of the program.
.. code-block:: vala

public class Main {
public static void main(string[] args) {
//...
}
}

The last lines simply end the definitions of the method and class.

Compile and Run
---------------
Expand Down