lsmith
Junior Member

Posts: 35
Registered: 2006-7-24
Member Is Offline
|
posted on 2007-4-30 at 06:48 PM |
|
|
Dot notation for interfaces?
I've been playing around quite a bit with components and interfaces lately and found that _castInterface is pretty cumbersome in the case of a
component with multiple interfaces.
First, the _castInterface call does not form an lvalue, so I can't say _castInterface(obj, "IiFace").foo(). I have to create an IiFace variable and
put the results of the _castInterface into that before I can make the call.
Second, the _castInterface call requires the use of extra variables to hold the results. If a component supports more than a couple interfaces (not
uncommon), then each call to separate interfaces requires the overhead of extra variables and the calls to _castInterface.
If I could make calls on an object directly through the interface, that would be very convenient. "obj.IiFace.foo()" Then, if there were more than
one interface, I could just change the interface name and make the call "obj.IiFace2.bar()"
The problem with this idea is twofold.
1) If the component does not support the interface (as in the case of an older component not supporting a newer interface), the cast would return
null. Calling a method on a null reference is bad. Could the player "do the right thing" by not calling functions on null references? Would that be
the "right thing"? I can't think of a practical application (outside of debugging) of crashing the program due to a null reference.
2) If a component reference is already a particular type, then the call through the interface may be ambiguous. However, since interfaces are not
nested, the depth is restricted to one-deep. A call to IiFace obj.IiFace.foo() ought to be the same as IiFace obj.foo(). The latter call is the
current UJML syntax, but it is really a specific instance of the more general former call.
I leave it up to the Jedi Council to figure out whether this is a useful suggestion.
|
|
|
rknox
Moderator
    
Posts: 35
Registered: 2006-1-21
Member Is Offline
|
posted on 2007-4-30 at 08:41 PM |
|
|
You make a good point that _castInterface is cumbersome. It's also somewhat expensive since it involves string comparison at run time. If you need to
do a cast multiple times, say in a loop, it's much better to do the cast once outside of the loop and assign it to a variable that refers to the
proper interface.
UJML, as it currently stands, has no syntax for referring to an object. Variables can refer only to interfaces. We could add a cast operator like C++
and Java that would allow you to do something like:
| Code: | | x = ((IBar) iFooRef).barFunc(); |
This would get converted to a _castInterface call by the compiler. No performance gain, but admittedly nicer syntax. I think it has to be a run time
error if the cast fails. This syntax is declaring an intent to call a function, and in nearly all languages with the concept of a cast, a failed cast
would be a run time error of some sort.
I'm not sure when something like this will show up in the language, but we recognize the need to make components more efficient and easier to use.
Thanks for the suggestion.
Rich Knox
Senior Software Design Engineer
UIEvolution
rknox@uievolution.com
|
|
|
|