Kasper Peulen
2016-08-18 07:32:17 UTC
Not sure if I recall correctly, but I heard the optional/named parameter
syntax is being reconsidered for Dart 2.0.
I haven't see any proposals for that, let me do an informal proposal here:
Postional optional parameters:
Dart 1.0:
String say(String msg, [String from, String device = 'phone']) { ... }
Dart 2.0:
String say(String msg, String from?, String device? = 'phone') { ... }
Named optional parameters:
Dart 1.0:
enableFlags({bool bold, bool hidden: true}) { ... }
Dart 2.0 :
enableFlags({bool bold?, bool hidden? = true}) { ... }
Named parameters:
Dart 1.0:
enableFlags({@required bool bold, @required bool hidden: true}) { ... }
Dart 2.0 proposal:
enableFlags({bool bold, bool hidden = true}) { ... }
This seems to me, as the most straight forward way.
Types on the right/NNBD
With the proposed types on the right side, and non nullable by default, you
may want to write it like this:
say(msg: String, from: String?, device: String = 'phone') -> String { ... }
enableFlags({bold: bool?, hidden: bool = true}) { ... }
So now, it is more tricky, because a parameter can be optional and/or
nullable. I would say that a parameter is optional if it either has a
default parameter or if the type is nullable (or both).
Syntax for named parameters
Now there is one other thing. You may wonder, why do you propose the {...}
syntax for named parameters in Dart 2.0? And why a colon `:` instead of the
more natural `=` in the call? Well, there is a reason, I want to propose
also the following. Named parameters are just sugar.
enableFlags(bold: true, hidden: false);
This would desugar to writing:
enableFlags({"bold": true, "hidden": false});
Yes, this is a map. Named parameters are sugar for writing a map. So
actually, all parameters are positional now, where the last one can be a
Map that can be written with some sugar as named parameters.
So you may wonder, does this really matter anything? Who would write the
later? Well, I think it actually does matter, because it helps making it
much more natural to write javascript interop, because in javascript devs
often add an option object (Map in Dart) as the last parameter. This object
just serves as providing a bunch of named/optional parameters. With this
proposal, this would map very straight forward to Dart.
Can we generalize this?
If you think about it, this kind of asks to give the Map object to have a
more flexible type. Now with this proposal, here:
enableFlags({bool bold, bool hidden}) { ... }
This says now, that the enableFlags function has as parameter a Map that
must have two keys, the string bold and the string hidden, both of type
bool. So if we can declare the type of the Map so specific here, why not in
other Dart code?
What about this syntax (assuming right hand types in Dart 2.0):
var map: Map<{"bold": bool, "hidden": bool}> = {"bold": true, "hidden":
false};
Well this syntax seems quite okay, I think we can provide a very natural
sugar for this? Let's look a second at the syntax for named parameters:
enableFlags({bold: bool, hidden: bool}) { ... }
enableFlags(bold: true, hidden: false);
It seems clear to me that the sugar should look like;
var map: {bold: bool, hidden: bool} = (bold: true, hidden: true);
Okay, so we have here a very specific kind of Map object, where all the
keys are strings. Of course this is just a plain javascript object. And
this should map to a plain javascript object when compiled. This would even
give more advantages with javascript interop. For example, you could give
much more auto completion and type safety to the react package.
Thoughts?
syntax is being reconsidered for Dart 2.0.
I haven't see any proposals for that, let me do an informal proposal here:
Postional optional parameters:
Dart 1.0:
String say(String msg, [String from, String device = 'phone']) { ... }
Dart 2.0:
String say(String msg, String from?, String device? = 'phone') { ... }
Named optional parameters:
Dart 1.0:
enableFlags({bool bold, bool hidden: true}) { ... }
Dart 2.0 :
enableFlags({bool bold?, bool hidden? = true}) { ... }
Named parameters:
Dart 1.0:
enableFlags({@required bool bold, @required bool hidden: true}) { ... }
Dart 2.0 proposal:
enableFlags({bool bold, bool hidden = true}) { ... }
This seems to me, as the most straight forward way.
Types on the right/NNBD
With the proposed types on the right side, and non nullable by default, you
may want to write it like this:
say(msg: String, from: String?, device: String = 'phone') -> String { ... }
enableFlags({bold: bool?, hidden: bool = true}) { ... }
So now, it is more tricky, because a parameter can be optional and/or
nullable. I would say that a parameter is optional if it either has a
default parameter or if the type is nullable (or both).
Syntax for named parameters
Now there is one other thing. You may wonder, why do you propose the {...}
syntax for named parameters in Dart 2.0? And why a colon `:` instead of the
more natural `=` in the call? Well, there is a reason, I want to propose
also the following. Named parameters are just sugar.
enableFlags(bold: true, hidden: false);
This would desugar to writing:
enableFlags({"bold": true, "hidden": false});
Yes, this is a map. Named parameters are sugar for writing a map. So
actually, all parameters are positional now, where the last one can be a
Map that can be written with some sugar as named parameters.
So you may wonder, does this really matter anything? Who would write the
later? Well, I think it actually does matter, because it helps making it
much more natural to write javascript interop, because in javascript devs
often add an option object (Map in Dart) as the last parameter. This object
just serves as providing a bunch of named/optional parameters. With this
proposal, this would map very straight forward to Dart.
Can we generalize this?
If you think about it, this kind of asks to give the Map object to have a
more flexible type. Now with this proposal, here:
enableFlags({bool bold, bool hidden}) { ... }
This says now, that the enableFlags function has as parameter a Map that
must have two keys, the string bold and the string hidden, both of type
bool. So if we can declare the type of the Map so specific here, why not in
other Dart code?
What about this syntax (assuming right hand types in Dart 2.0):
var map: Map<{"bold": bool, "hidden": bool}> = {"bold": true, "hidden":
false};
Well this syntax seems quite okay, I think we can provide a very natural
sugar for this? Let's look a second at the syntax for named parameters:
enableFlags({bold: bool, hidden: bool}) { ... }
enableFlags(bold: true, hidden: false);
It seems clear to me that the sugar should look like;
var map: {bold: bool, hidden: bool} = (bold: true, hidden: true);
Okay, so we have here a very specific kind of Map object, where all the
keys are strings. Of course this is just a plain javascript object. And
this should map to a plain javascript object when compiled. This would even
give more advantages with javascript interop. For example, you could give
much more auto completion and type safety to the react package.
Thoughts?
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.