Re: [Vala] Converting nullable to non-nullable types



On Fri, 2010-02-26 at 15:08 +0100, Jiří Zárevúcky wrote:
Sam Wilson píše v Čt 25. 02. 2010 v 21:58 -0500:

Would this also be a valid way to check for null?


T unbox<T>(T? v, string str)
{
    if (v == null)
            throw new MyError.FAILED(str);

    return (T)v;
}

I suppose it should work quite the same, but I'm not sure how does Vala
cope with nullable -> non-nullable casts.


When I try and compile the following code, I get this error:

"unnull.vala:17.9-17.62: error: Assignment: Cannot convert from `T' to
`GLib.Object'"

Is this a bug, or are templates meant to work like this?

[code]

errordomain MyError
{
        FAILED;
}

T unbox<T>(T? v, string message)
{
        if (v == null)
                throw new MyError.FAILED(message);
                
        return (T)v;
}

int main(string[] args)
{
        Object? nullable = new Object();
        Object not_nullable = unbox(nullable, "The object was null!");
        
        return 0;
}

[/code]

Attachment: smime.p7s
Description: S/MIME cryptographic signature



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