Archive for February, 2006

Casting

Monday, February 20th, 2006

Interesting thought after talking with MDK on #mono tonight. Casting in CLI is a fun quirky mess. When trying to explain it, with implicit casting and explicit casting, the ‘isinst‘ and ‘castclass‘ and the like don’t really help explain it. Neither do the optimizations of the various compilers and each of the casting quirks of each language.

Specifically in C# you have a few options, such as the “as” keyword or the “(…)” operator. If the compiler can’t optimize the code block in some other way or if an implicit/explicit function conversion isn’t specified, the ‘as’ keyword will generate the “isinst” code, where as the “(…)” operator will generate the “castclass” code. The biggest difference from a pure IL perspective (not excluding how C# allows you to get here) is that ‘castclass’ will check the type and throw an InvaildCastException if it can’t cast, where as ‘isinst’ will simply pass back a null if it can’t cast the value for you.

This also explains why you can use a “(…)” operator to cast a value type but why you can’t use the “as” keyword on them. Since the ‘as’ keyword would try to generate the ‘isinst’ code, if the cast failed, it would try to return null, and in .NET, value types can never be null. If you are trying to be proactive to prevent as many exceptions from being thrown as possible (since exceptions are such resource hogs), then it seems better to use the ‘as’ keyword on all your object types and checking to see if they are null after casting, if its necessary. Be careful though. Finding a Null exception is much more difficult then tracking a casting exception when it happens. If its a value type you are trying to convert such as trying to convert from an int to a short maybe, then you might be stuck with some manual checks for things like overflow (like maybe ‘ if( x < Int16.MaxValue ) …; ‘ ) or whatever.

Now I’m wondering how this all changed with 2.0 where you can now have nullable types? Looks like I’m going to be hacking tonight. :-)

Motivation/Inspiration

Monday, February 13th, 2006

Just finished reading a thank you letter from a little project I sent a patch to, to help them with their GDI issues on Win32.

I love these little patches I write for all the little projects out there. I manage to shoot off at least two or three a week on average. It’s funny to get some of the reactions I get sometimes to my patches. Some want me to sign something about the origin of the code or to sign over copyrights, some want me to do it over in some other coding standard they use, some take what they can get because they are happy that anyone else even spent the time to look at their code and are willing to share the responsibility for it. Some patches never see the light of day, and some suffer from bit-rot because the person I contribute back to “has other plans” and redesigns the entire concept. It’s nice though. You get your name in someone’s change log, help file, about screen, or sometimes your patch gets lost in the entire mix. It’s nice though because you can go back and google for your name and see all the places you turn up later. :-)

I recommend everyone do it… Oh and ignore those egotistical programmers that rejected your code because they want 100% perstine crap. Don’t let them break your spirit. Stick with what you know too, and know that no application has ever been written without flaws. If everyone thing was perfect, why would we need alpha and beta code and would we ever really need a 2.0 release?

Happy Valentines Day
(I know I’ll be working overtime… it’s one of those big days at Match.com, yah know… :-))

Just Cuz!

Monday, February 13th, 2006
I’m hearing some talk about Javascript 2.0 (ECMAScript 4?). There is a page on Mozilla’s site about it. It’s amazing that we are still using Javascript 1.5. Its getting about 6 years old now. Now with everyone buzzing over getting AJAX capable sites, we really need some real and true OOP support to make this stuff a little easier. JScript .NET isn’t bad (by the way, Mono’s mjs will compile Jscript.net/Javascript code to IL if your interested). A lot of people might argue that you can emulate OOP in Javascript, and those methods of doing OOP are not that bad really, but its not so clear for some people just getting it into it. There are so many ways to do it, and just about every method lacks some key points of OOP that I really want like abstraction and type safety, although since the code doesn’t really get evalutated until runtime, it doesn’t really matter, unless we get some compile time (or pre-emptive) checking in there as well. Embedding mono into mozilla might be a solution worth looking into. :-)

On a similar topic, I’ve been tinkering and I have come up with a working extention to our WebService classes to support XML-RPC (in addition to the already included WSDL generation and SOAP support, and the basic http POST/GET wrapping providers). I also have an update to the documentation screen to support generatting AJAX based Javascript clients for you (linkable directly in your page no less even though it needs some caching support to make it a bit nicer), similar to how we generate code samples for C# and VB, just to support more clients out of the door. My XML-RPC support is a little flaky right now (having some datetime and some array type issues on the php and perl clients I’m testing with) but since I need it for an upcomming project, I should have it working soon.

Going through the process of getting mod_mono working again on my server. I was having trouble with my distro’s shared module support, and after getting tired for the night, I started looking at how tomcat connects into apache, and I found that its not much different from the method in mod_mono. In fact 2 of the methods listed on their site will already work with mod_mono right now (mod_proxy for one). Now that mod_cache and mod_mem_cache are production quaility (according to Apache :-)) in Apache 2.2, we can now take advantage of that. Apache’s libapr (apache portable runtime) isn’t that bad after closer inspection. It provides something that most apache modules had to do on their own (like making n http request on its own or handling memory), although its not limited to just use in apache modules (I know subversion uses it for something). Might be worth looking at it for that next project.