Conversion Operators, Typecasting and You

| | Comments (2)

I learn new things about REALbasic just about every day. Some of them make sense at first glace and are pleasant surprises. Otherwise violate the principle of least surprise and annoy me at first (until I figure out the reasoning behind them). I ran into one of these today. I had code that looked like this:
[rbcode]
Class Foobar
Function Operator_Convert() As Dictionary
return new Dictionary
End Function
End Class
[/rbcode]
Of course, it had more code in the function, but that's immaterial since the focus is on the overloaded operator itself. The above code is also 100% correct -- it defines a conversion operator from a Foobar to a Dictionary. However, the below code has an error in it.
[rbcode]
Dim var as Variant = someFoobarObject
...
SomeFunctionTakingADictionary( Dictionary( var ) )
[/rbcode]
Did you spot the error? ;-) It's the Dictionary( var ) part (better known as a typecast). I was under the impression that I needed to tell the compiler "no, this variant isn't a generic object, it's a Dictionary", which would then cause the compiler to call my conversion operator since it knew the type of the object in the variant (which is a Foobar) and see that it was to be turned into a Dictionary (and hence, call my conversion operator). However, REALbasic doesn't work this way -- and it ended up generating an IllegalCastException for my troubles. So why is this?

After thinking about it for a while (and consulting Mars about it), it made sense. Conversion (the operator I overloaded) is the act of taking one type and turning it into a totally dissimilar type. In this case, I am morphing a Foobar into a Dictionary. Everything about the static type of the object is changed, including the object reference. However, typecasting is the act of telling the compiler about an upcast. Basically, it's saying "this object is really a subclass of that object". The dynamic data is all still retained, the structure isn't changed in any way, and the object reference is the same. Basically, they're two totally different operations.

So, to fix the above code to make it work, I simply need to assign the Variant object (holding a Foobar) into a Dictionary reference. Basically, I can take out the typcast and pass it in as a Dictionary because the compiler will look for the conversion operator before complaining about the types not matching. The code is simpler to look at (though less explicit) and functional.

Now if only it hadn't taken me a decent while to figure out what was wrong with my code!

2 Comments

Yeah, it'd be nice if we had an explicit conversion operator, like VB's CType().

Yeah, that would be nifty. What we have now gets the job done, but it can be tricky if you're not careful about it.

Leave a comment

Disclaimer

I'm currently an employee of REAL Software. My blog is mine. The opinions represented in this blog are mine as well and may not represent my employer's opinions. All original material is copyrighted and property of the author.

REALbasic® is a registered trademark of REAL Software, Inc. REAL SQL Server™ and Lingua™ are pending trademarks of REAL Software, Inc. All rights reserved.