missing optional positional arguments

19 views
Skip to first unread message

John Yendt

unread,
Nov 14, 2017, 10:12:18 AM11/14/17
to Dart Core Development

There was some talk of letting functions take both optional positional arguments and optional named arguments.  During the discussion I thought about functions I had written early on, but got the optional positional arguments in the wrong order,  Of course fixing it later becomes a big editing, and possibly dangerous, task.  So, consider this:

void foo(int i, [int x = 5, int y = 3, int z = 10]) {
    // do something
}

If you want to call the function only specifying the y value, but let the x and z keep their defaults, then

foo(4,,7);

The empty commas indicate that that optional positional argument x should take it's default.

Currently, you are required to put in a value, and 

foo(4,5,7);

is equivalent.

The problem is that the function writer may want to change any of the defaults.  If the defaults need changing, then callers may need to go around and fix leading optional positional arguments that they filled in with defaults just to use a later argument.  In the case of foo(4,,7); the first default can change without breaking the callers.

Just a thought, maybe worth discussing.

Thanks
Reply all
Reply to author
Forward
0 new messages