AsymLabs
AsymLabs

Reputation: 963

Genie Vala Generics and Nullable Types

Simple question: In the following generic class, how should the generic type and contained types be defined so that they are nullable? The following will not compile.

class Pair? of G1, G2: Object

    _first:G1?
    _second:G2?

    construct()

        _first = null
        _second = null

    def insert( first:G1, second:G2 )

        _first = first
        _second = second

    def insert_first( value:G1 )

        _first = value

    def insert_second( value:G2 )

        _second = value

    def second():G2

        return _second

Usage:

var pair = new Pair() of string, string
pair = null

Upvotes: 1

Views: 500

Answers (2)

AlThomas
AlThomas

Reputation: 4289

I can't wrap my head around the concept of a type parameter that has null as the type. I don't think that is a useful concept. So the definition of your class would be:

[indent = 4]
class Pair of G1, G2: Object

    _first:G1?
    _second:G2?

    def insert( first:G1, second:G2 )
        _first = first
        _second = second

    def insert_first( value:G1 )
        _first = value

    def insert_second( value:G2 )
        _second = value

    def second():G2
        return _second

If you must re-assign the variable that has the object instance to null then it would be:

[indent = 4]
init
    var pair = new Pair of string,string()
    pair = null

The Vala compiler will, however, dereference pair when it goes out of scope. So I'm not sure why you would need to assign null.

The use of nulls would ideally only be used when interfacing with a C library in my view. Accessing a null can lead to a crash (segmentation fault) if it is not checked for properly. For example:

init
    a:int? = 1
    a = null
    var b = a + 1

The Vala compiler does have an experimental non-null mode that does some checking for unsafe code. If you compile the following with the Vala switch --enable-experimental-non-null:

[indent = 4]
init
    var pair = new Pair of string,string()
    pair = null

you will get the error:

error: Assignment: Cannot convert fromnull' to Pair<string,string>'

If you understand the consequences then you can tell the compiler this is OK with:

[indent = 4]
init
    pair:Pair? = new Pair of string,string()
    pair = null

Upvotes: 2

Jens M&#252;hlenhoff
Jens M&#252;hlenhoff

Reputation: 14873

Due to the way Vala Generics work, generic parameters are always nullable.

As long as you don't switch on --enable-experimental-non-null class variables are nullable as well, so your code simplifies to:

[indent=4]
class Pair of G1, G2: Object

    _first:G1
    _second:G2

    construct()

        _first = null
        _second = null

    def insert( first:G1, second:G2 )

        _first = first
        _second = second

    def insert_first( value:G1 )

        _first = value

    def insert_second( value:G2 )

        _second = value

    def second():G2

        return _second

init
    var pair = new Pair of string, string
    pair = null

When --enable-experimental-non-null is on, you have to be explicit in the type of the variable. I don't know how to write this in Genie, I tried this, but the compiler does not like it:

init
    pair: Pair? of string, string = new Pair of string, string
    pair = null

In Vala it's no problem:

class Pair<G1,G2>: Object {
    private G1 first;
    private G2 second;

    public Pair () {
        first = null;
        second = null;
    }

    // ...
}

int main () {
    Pair<string, string>? pair = new Pair<string, string> ();
    pair = null;
    return 0;
}

Upvotes: 2

Related Questions