S
Simon L. Prinsloo
Guest
A type conversion like CAST(), INT() LOGICAL(), DECIMAL(), DATE(), is used to signal to the compiler that I know that data types are not compatible, but that I believe that it will be possible to convert it. The compiler say "Ok." and let it go. The runtime, on the other hand, will check again and throw an error if I was wrong. Sometimes the compiler knows for sure it WILL NEVER WORK, and stop me: DEF VAR AS myVar LOGICAL INIT TRUE NO-UNDO. MESSAGE DATE(myVar). Other times the compiler knows it COULD work and leave it up to the runtime catch me if I make a mistake: DEF VAR myVar AS CHARACTER INIT "Hi" NO-UNDO. MESSAGE DATE(myVar). The same should be true when I use CAST(), but it is not. CAST() is inconsistent with the rest of the language. Thus I argue that this is a bug. With CAST() the compiler does not allow every version of POSSIBLE, as it should, but block us even when the CAST() might be possible. When both the source and the target are CLASS types, the compiler knows for sure that the CAST will only succeed if either: The source type is in the inheritance hierarchy of target type. The target type is in the inheritance hierarchy of target type. However, as soon as either one or both of the source and the target types is an interface, the compiler has no way to know if the runtime instance held in an interface type will be from a specific class hierarchy, or that any instance in a class type will not be a (possibly derived) type implementing the interface. It only knows it COULD be. Thus, similar to other type conversion, CAST() should signal to the compiler to "skip this check" and the runtime must double check. For example, the compiler can stop me from trying to convert an Eagle to a Cat, or a LOGICAL to a DATE, but it cannot stop me from converting STRING to a DATE or Mammal to a Dog. It is the runtime's job to stop me if my Mammal is actually a Cat. Yet the compiler it knows Cat is not a Dog, as neither one derives from the other. This works correct. On the other hand, it is should be up to the runtime to figure out that the Bat in IFlying can be assigned to the Mammal variable or that the Eagle in IFlying can be assigned to a Bird type, but not a Mammal type. Also, the runtime must determine that the Bat in the Mammal variable or the Eagle in the Bird variable is an IFlying but the Ostrich in the Bird variable or the Elephant in the Mammal variable are NOT IFlying. The compile cannot know and should thus allow it.
Continue reading...
Continue reading...