Re: [Vala] Using 'as' for casting?



On Fri, 2009-08-07 at 17:38 +0200, Jiří Zárevúcky wrote:
On 08/07/2009 05:19 PM, Daniel Svensson wrote:
When converting my application to use Gtk.Builder I ended up with a
lot of code like this:

var lbl = builder.get_object("foo") as Gtk.Label;

And this caused me to try the following:

var time = Time.gm (unxtime as time_t);

as it was more readable than:

var time = Time.gm ((time_t) unxtime);

This however is not valid syntax, but maybe it should be? On IRC I got
the answer that if an 'as' would fail it would return null, and that's
why it shouldn't work.

I thought it was best to drop a mail to the list and see what people think.

   

That's right, but it's not just the fact it can return null. You can't 
use 'as' for simple types because there is no way to do run-time type 
check for them, so no way to find out whether cast is valid (until the 
segfault, of course). So there is no way 'as' could work for them.

So the difference between 'as' and '(Type)' is that
(1) 'as' is run-time, aka, dynamic
(2) '(Type)' is compile-type, aka, static

For structs doesn't carry sufficient type information for a run-time
casting, '(type)' casting is the only reasonable way for it.


Yu
_______________________________________________
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]