Re: [Vala] Boxed type generics




Even known it's doesn't sound like that, and it's work in .Net, double? is always
a pointer type. It's the same as double*. This explains the first example error.
In the second one, you convert some type to double* and then to double(in the
deceleration "double tmp"). The compiler seems to ignore the first conversion,
and skip to the second.

In conclusion, use (double) instead of (double?). If you want to achieve a null
passing in by value(i.e. not pointer), pass another Boolean which tells if it's
empty.

Yours,
Tal

Date: Mon, 19 Mar 2012 13:59:33 +0100
From: stephan ecshi net
To: vala-list gnome org
Subject: [Vala] Boxed type generics

Hello,

I'd like to do something like the following (somewhat oversimplified):

public T increment<T> (T number) {
        return (double?) number + 1;
}
public static void main () {
        stdout.printf("%f\n", increment<double?> (4.7));
}

Unfortunately the C-compiler complains about not being able to convert
the double to a pointer type in the return statement.
I have found out this works as expected:

public T increment<T> (T number) {
        double tmp = (double?) number;
        tmp += 1;
        return &tmp;
}

This, however, seems not to be the right thing to do. Could someone
explain how to handle those boxed types correctly without resorting to
the address-of-operator?

Thanks,

Stephan

_______________________________________________
vala-list mailing list
vala-list gnome org
http://mail.gnome.org/mailman/listinfo/vala-list
                                          


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]