Overloading method names in REALbasic

It wasn't obvious to me that REALbasic allows overloading method names, where you use the same name on two or more methods but with different sets of parameters (arguments).

It turns out we can indeed overload method names. The REAL Studio 2010 IDE even helps out along the way.

Create a method and name it as you normally do. The method appears in the list. For example, "PhoneHome", a method to report problems at runtime. This method has two arguments, both String, one containing a message of the problem, and the other containing the name of the method.

After some use, we realize we commonly repeat code when reporting RuntimeExceptions being thrown. In each use we extract the Exception's ErrorNumber, Message, and Stack. Rather that copy-pasting similar code, we should move that code into a single method where we pass the Exception object rather than text. For simplicity it makes sense to use the same method name.

So we create a new method, but use the same name "PhoneHome". Immediately REAL Studio assists by adding the argument types to the listing display of method names.

Now we can call either method by the same name. The REALbasic compiler and runtime knows which one we want by looking at the type of the passed parameters.

When scanning the list of methods, we immediately recognize the display of parentheses and arguments as meaning a method is overloaded.

No comments:

Post a Comment