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



Sam Wilson píše v Čt 25. 02. 2010 v 18:07 -0500:
Hey!

I am just wondering if there is any syntactic sugar for shortening the
following code segment

[code]
Element? nsource = ElementFactory.make("filesrc", "file-source");
if (nsource == null) { printError(); return; }
Element source = (Element)nsource;
[/code]


To me, it seems like a common pattern to convert a nullable type to a
non-nullable type.  If such a shortcut does not exist, perhaps it would
be a good idea to add a "conditional cast" operator


Here's a little trick you might like:

[code]

errordomain MyError {
        FAILED;
}

T throwerror<T> (string str) throws Error {
        throw new MyError.FAILED (str);
}

int func ()
{
        int? i = null;
        int j = i ?? throwerror<int> ("hello");
        return j;
}

[/code]

Attachment: signature.asc
Description: Toto je digitálně podepsaná část zprávy



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