Re: cast and assert
Daniel Bonniot <[email protected]> Thu, 25 Mar 2004 12:19:46 +0100
| Newsgroups | gmane.comp.lang.nice.general |
|---|---|
| Message-ID | <[email protected]> |
Hi Roman, > I am a newcomer to Nice and I am trying some of its features. > > Welcome! >I have read an wiki page about cast >http://nice.sourceforge.net/cgi-bin/twiki/view/Dev/NiceCasts >and have some comments. > > There are two cases for cast usage: >1) you do not know if the cast is valid >that situation is clear you have to check with instanceof: > A a=new B(); > if(a instanceof B) println(a.b); > > Yes, that's a good way to handle this case. Another one is to use a multi-method to select behaviour based on the class, which can be preferable if there are several cases. >instead of > A a=new B(); > printlln(cast(a).b)); >2) you are sure that the cast is valid ( from some other logic) >in that case one can use assert in the same way like if statemant >before: > A a=new B(); > assert a instanceof B; > println(a.b); // not compiling now with nice 0.9.7 >instead of using cast. One could also use requires for that purpose. > In that way you can avoid using explicit cast almost everywhere. > > Does that make any sense? > > Yes, that's a very good idea. I agree this is better than the current syntax because it explicitely states which type you are expecting. The C/Java cast syntax also has this property, but it's not particularly elegant. Furthermore, it's a good idea to make this situation stand out as an assert, since it is indeed an assertion about the behaviour of the program. Besides, it should be possible to avoid most casts by using multiple dispatch, covariant return types, parametric types, ..., so it's no problem to make the remaining cases a bit more verbose, which also encourage you to find a safer way if possible. I definitely think this should be implemented before version 1.0. Maybe you could open a feature request? Daniel ------------------------------------------------------- This SF.Net email is sponsored by: IBM Linux Tutorials Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click