So I learned something new while talking to Mars today. Did you know that REALbasic's implementation of optional parameters is one of the most powerful around? In any language?
The way they work is like this: the compiler looks at any possible method signatures and figures out which one to call with what parameters going where. If there's only one logical choice, then that's called. But if there are multiple choices which are equally good, then we throw an error.
So, this means that you can have optional parameters anywhere in the method signature. For instance:[rbcode]
Sub Foo( test as Integer, wahoo as String = "Wahoo", awesome as String, neato as Integer = 0 )
MsgBox CStr( test )
MsgBox wahoo
MsgBox awesome
MsgBox CStr( neato )
End Sub
// Call it like this:
Foo( 1, "Neato" )
// Or like this
Foo( 1, "Yippee", "Neato" )
// Or even like this
Foo( 1, "Yippee", "Neato", 42 )
[/rbcode]So how does this work if you overload the method? As long as the call isn't ambiguous, it still just works! So let's say we add another method called Foo that has a different signature to it, like this:[rbcode]
Sub Foo( test as String, wahoo as Double = 1.2, awesome as String = "Awesome", neato as Integer )
MsgBox test
MsgBox CStr( wahoo )
MsgBox awesome
MsgBox CStr( neato )
End Sub
// Call it like this:
Foo( "Neato", 1 )
// Or like this
Foo( "Neato", 25.6, 1 )
// Or even like this
Foo( "Neato", 25.6, "Yippee", 1 )[/rbcode]
It all just magically works!
If you've got a C++ background, then you can surely appreciate just how powerful REALbasic's system is. In C++, the only optional parameters you can have must come at the end of the method signature. What's more, if you have two optional parameters, that means you must specify them both if you really only want to specify the last one. For example:
// Here's the delcaration
void Foo( int bar, const char *baz = "Test", double wham = 1.2 );// Here's how you call it
Foo( 1 );// Or
Foo( 1, "Foobar" );// But what if you just want to call it with the last parameter and not the second one?
Foo( 1, 12.5 ); // COMPILER ERROR!// You have to do it like this instead
Foo( 1, "Test", 12.5 );
So REALbasic rocks the pants off C++ when it comes to working with optional parameters. Who's the toy language NOW, huh? ;-)
That is pretty cool. I'm not sure I'd use it very often, but the smarter the compiler is, the better!
> Who’s the toy language NOW, huh? ;-)
Lisp?
Off topic, but one cool thing I found my accident today (not sure if it's documented or not) - if you cmd-click (on mac at least) on a constant in your code, it'll open a new window and select the constant; very helpful for global constants in a module or such. Well...at least I thought it was neat :)
@Brad -- never used Lisp, so it must be a toy language, right? ;-)
@Chad -- Check out this posting from a few days ago. :-)
http://ramblings.aaronballman.com/?p=699
Thanks...always a day too late - how'd I'd miss that?!. Glad to see the continuing emergence of odd little nice features though!
Aaron rocks more like it!
I have to admit, Aaron does have rockative-type qualities.
With all this fun, who needs function overloading?
http://docs.python.org/ref/function.html
@DeanG -- interpreted languages don't count. They're just toys. :: grins, ducks and runs :: ;-)
Am I missing something, or aren't these really default parameters rather than optional parameters? A default parameter may be set explicitly by passing its value to a method; not passing the value would cause the default value to be used. An optional parameter would be one that can be omitted completely (without even a default value specified).
In REALbasic (as in many compiled languages), the concept is the same.
Scott: you can use the "Optional" keyword if you don't want to specify a default value, like "Optional param As type". I don't remember which version of RB we added this in.
The Optional keyword just specifies the default as the "zero" value though, right? I think what Scott was driving at is that in some languages, there's a difference between the default value and not specifying a value at all. For example. VB has the IsMissing keyword which lets you tell whether the user passed the default or didn't pass anything at all.
Hi Aaron,
yes I found that a few months ago and find it great for adding variables to a method latter in the developement of a program. Say I have a method called printme(x as integer,y as integer, s as string) and write 10,000 lines of code and then decide to add in justification I just change the method to
printme(x as integer,y as integer, s as string, j as string ="C") which means I dont have to find all the other calls to the printme method.
I havnt tried it but would this work
foo( i as integer=1, s as string="test")
foo("didnt give the integer value at all")
would RB know that i=1
Might try later today.
Thanks
Damon