Type conversion vs casting

| | Comments (2)

In REALbasic, there are several ways to get data from one format into another format. But there are two main terms used to describe the process: type casting, and type converting.

You're probably already used to implicit type conversion without ever even knowing it. This happens when you assign a value of one type to a variable of a different type. For instance: dim i as Integer = 3.2 This code implicitly converts from a Double to an Integer.

In 2008r3, we added a new operator to the language called CType -- this is an explicit conversion operation. It behaves almost identically to implicit conversion in that it converts data from one format to another. However, it has some extra functionality bolted onto it. For instance, it can also cast data from one type to another. Furthermore, it allows for conversions that even the implicit conversion operator doesn't allow (such as converting between enumerations and other types, or from one structure to another structure). Finally, the CType operator is the way you tell the compiler "I really know what I am doing, so please don't warn me if the conversion might lose precision, or sign, etc."

Typecasting is what happens when you use the typecast operator, like this: EditField( someRectControl ).Text = "Foobar". In this code, someRectControl is of type RectControl, and we're casting it to be an EditField.

So let's get down to the technical information -- what's the difference between casting and converting? In REALbasic, casting means "same bits, different meaning." So, in the instance of casting a RectControl to an EditField, this makes sense. We have a reference to a something-or-other. If its static type is RectControl, but we really mean it to be an EditField (and it really is!), then we can cast it to an EditField. The reference is still the same under the hood. On the other hand, conversion means "different bits, same meaning." In the example of converting a double to an integer, the bits are totally different between the two -- but the meaning is the same. You're saying "convert this number format to that number format", but the meaning is "I have a number, and I just want a different representation of that number."

When you cast something from one type to another, you will never lose information. You're simply telling the compiler to treat a piece of data as if it were another type. However, when you convert something from one type to another, you run the risk of losing information. Implicit conversions are dangerous like this, and so you can expect a future version of REALbasic to warn you of possible problems. However, explicit conversions are equally as dangerous -- but they're also explicit. The assumption when you use CType is that you know what you're doing, even though it is a possibly lossy operation.

2 Comments

So, can I finally cast a Single into a Int32, or a Int32 into a object reference?

@Thomas -- you can cast a Single to an Int32, yes. You cannot (and never will be able to) cast an Int32 into an Object or vice versa.

Leave a comment