3 question about vala test/objects/fields

I found a strange thing here.
First of all, there’s 2 main () here, and the program doesn’t compile because of that.
The second where class Foo? there are classes Maman.Bar Maman.Foo Maman.Ibaz, and Maman.Bar inherited from Foo.
And third, what does a class Declaration like int field(?)

public class int public_class_field = 23;

Like to be treated like a normal int, not a class.

The test runner script compiles the tests with --main main, which specifies main as the entry point.

This one surprised me a little bit, but it seems that:

class Foo.Bar : Object {}

is exactly equivalent to:

namespace Foo {
    class Bar : Object {}
}

The Vala manual (see below), says “When declaring which class, if any, a new class subclasses, and which interfaces it implements, the names of those other classes or interfaces can be qualified relative to the class being declared.” I couldn’t find anything that covers how Foo is used in Maman.Bar.main though.

class is modifying the field declaration, making it shared between all instances of the class (and the class itself): Projects/Vala/Manual/Classes - GNOME Wiki!

1 Like

Thanks for the replies. Before you get too far, another small question is how to access the inherited class field. A guide to the Gnome said about the access functions inherited class through base keyword , but nothing is said about the fields.
This is what it looks like on D lang.

class A { int a; int a2;}
class B : A { int a; }

void foo(B b)
{
    b.a = 3;   // accesses field B.a
    b.a2 = 4;  // accesses field A.a2
    b.A.a = 5; // accesses field A.a
}

You can cast to the parent type:

class Alpha {
    public int a;
    public int a2;
}

class Beta : Alpha {
    public new int a;
}

void main() {
    var b = new Beta();
    
    b.a = 3;   // accesses field Beta.a
    b.a2 = 4;  // accesses field Alpha.a2
    (b as Alpha).a = 5; // accesses field Alpha.a
}

(Vala didn’t like A and B as class names)

This can also be used if you define a non-virtual method that hides a parent method: https://wiki.gnome.org/Projects/Vala/Tutorial#Method_Hiding

base is used to chain up with virtual methods and constructors.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.