Rae PL – named function arguments and defaults?

Hello.

No feedback yet. Well, that’s understandable, as I’m just shouting to the internet in the middle of the night without telling anyone to come and listen.

Anyway. This is going to be my first question, and you get to directly influence the syntax of Rae programming language. All you have to do is answer the question in the comments.

C++ has something that I’d call function argument parameter defaults that only work when defining the class header. It’s awfully painful to strip all the defaults away when doing the implementation part into the .cpp file. And again when you’re calling the function (or method in C++-speak), you’re forced to strip away the argument types and names.

An example:

//in the class definition you might have a method:
int someMethod(int set_age = 45, int set_points = 0, int how_many_pianos = 1, Button* dont_push_this_button = 0, bool do_the_stuff = true, bool what_about_other_stuff = false, int nro_fish = 5, int nro_rodents = 0);

//and then in the .cpp implementation you'll have to strip the params and
//add the class name to every method.
int SomeClass::someMethod(int set_age, int set_points, int how_many_pianos, Button* dont_push_this_button, bool do_the_stuff, bool what_about_other_stuff, int nro_fish, int nro_rodents)
{
...
}

//Ok. And then you call the method later:
if(someObject) someObject->someMethod(47, 32, 2, myButton, false, true, 129, 62);

Wow. When I’d 2 years later read the function call, I’d be forced to look at the definition of someMethod, to see what are all those ints and bools doing, and what do they affect.
Ofcourse it is possible to write it as a comment, but that kind of duplicates the line.
Or then you do

someObject->someMethod(/*set_age:*/47, /*set_points:*/32, /*how_many_pianos*/2, /*dont_push_this_button*/myButton, /*do_the_stuff:*/false, /*what_about_other_stuff:*/true, /*nro_fish:*/129, /*nro_rodents:*/62);

But somehow that doesn’t look so pretty.

Also it really annoys me when I’m using my beloved copy-paste keyboard shortcuts, that I actually have to strip so much info out of the function definition just to call it.

In Rae I’m hoping we can change that. In Objective-C you can have named parameters in function calls like:

[someObject setAge:47 set_points:32 how_many_pianos:2 dont_push_this_button:myButton do_the_stuff:false what_about_other_stuff:true nro_fish:129 nro_rodents:62];

And that looks so much better than the C++ version. It is a self-documenting function call.

So, because Rae is still going to look more like C++, C, Java, D and the curly brace family, in Rae a function call is going to look something like this (also notice the included argument types in the function call):
option 1:

someObject.someMethod( int set_age = 45, int set_points = 0, int how_many_pianos = 1, Button dont_push_this_button = myButton, bool do_the_stuff = true, bool what_about_other_stuff = false, int nro_fish = 5, int nro_rodents = 0 );

or option 2:

someObject.someMethod( int set_age: 45, int set_points: 0, int how_many_pianos: 1, Button dont_push_this_button: myButton, bool do_the_stuff: true, bool what_about_other_stuff: false, int nro_fish: 5, int nro_rodents: 0 );

So, now you get to decide which one is it going to be! Tell me in the comments.

But which ever character is chosen (the equal sign or the colon), the function definition will still use the same way of defining the defaults. That way it will be just a copy paste when you want to use the function in it’s default form (in case all it’s arguments have default parameters…).

Here’s what that same definition would look like in Rae, if we took the Option 1.

func (int nro_bottles_i_have_to_drink) someMethod (int set_age = 45, int set_points = 0, int how_many_pianos = 1, Button dont_push_this_button = what_should_i_put_here_it_cant_be_null, bool do_the_stuff = true, bool what_about_other_stuff = false, int nro_fish = 5, int nro_rodents = 0)
{
...
}

I really love copy pasting the function definition and then just moving along without much editing. How does this sound to you?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s