-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Meta: Small and useful features collection #1077
Comments
May I suggest adding #620 to the list? Otherwise, I'd vote for:
These two gives significant readability increase. Named everywhere fits nicely with Flutter. And suffix awaits are a very common use-case I'd also argue that:
don't seem valuable to me. The ones I haven't mentioned yet looks interesting. Not game-changer, but I can see myself using them. All in all, the way I'd prioritize it for me, ignoring how long they take, would be in order:
|
The link for |
Please add short assignment!
|
That could be similar to #831. |
I don't thing #620 is in a state where it's clear what the solution would be. Multiple features are discussed, including omitting/eliding one type parameter from a method taking more than one, or allowing pattern matching to extract nested types from a single parameter type. The former might be a small feature once it's properly designed, depending on the design, the latter definitely won't. |
Is #336 a lot of effort to implement? It would make naming classes in large codebases much easier because many classes could be nested within others. Right now we find that we have to name classes with really long names to avoid conflicts and/or confusion. |
@samandmoore Yes, #336 (nested class declarations) is probably non-trivial. |
Waiting for the |
For me the top three would be:
But even more useful than #323 would be let expressions in collection literals (the idea is mentioned in #323 and perhaps narrowly defined enough that they could be considered as part of this discussion). Apart from that, shorter function expressions at least for getters (instead of |
Much as I'd love a |
I would strongly favor a let expression in the example you gave. Not only is it likely to be a more familiar construct to users coming from other languages, but I'd argue that it also fits better into the mental model of the current collection literal design (since you just have to remember that the three constructs final ifExample = [if (a == b) c];
final forExample = [for (final a in b) c];
final letExample = [let (final a = b) c];
final letExampleWithMultipleBindings = [let (final a = b, final c = d) e]; Right now, trying to develop in a declarative style using lots of immutable objects is still quite a pain in Dart, with the exception of collection literals, which really shine in these scenarios. But it is currently frustrating to constantly encounter the road block of having to use the same expression twice and then having to factor out what could have been a declarative collection into a more imperative version. |
|
I think the shorthand for import/export is a home run. It makes code a lot simpler to understand for those coming from Python/other modern languages; it's also just simpler to type and read. |
How about optional semicolon? |
I doubt this is a "small" feature. |
@lrhn what about context based enum member resolution (similar to Swift), where instead of |
@mraleph While I very much like the idea of enum value shorthand (#357), I don't think the idea is well-defined or mature enough to make this list yet. It's not clear how it should handle (I notice that I already prematurely gave it the label, so I've removed that again). |
Replied here: #357 (comment) |
For me definitely Named arguments everywhere (#1072) then we might be able someday to get rid of the named I don't know if that is possible without too much work. I would like to see a |
What about Named Arguments Shorthand #1123? Specifically this proposed syntax: #1123 (comment). It seems like it should be trivial to implement (only adding a bit of syntax and a front end transformation). |
Please be stupid, Please be simple. |
Might I recommend #112? It seems simple enough to implement. |
I don't think #112 is trivial enough, mainly because of the typing. If you write Another option is to just say that if you have more than one type in an So, until we can get a decision on that design issue, #112 won't be ready for this list. |
Is the checkbox list up top rather final? Will these all be guarded by one language experiment flag? |
Definitely not one experiment flag. We plan to interleave these with larger features when we can find the resources. The criteria for which goes first depends on which resources we have, and if there is an urgent need for something. I hope we'll get them all eventually. :) |
Any plans about constant RegExp literals? scanner.scan(/(\s|\/|>)/); |
No plans for constant If you really need a constant class ConstRegExp implements Pattern /* or RegExp if you're ambitious */ {
static final _cache = Expando<RegExp>();
final String _source;
final bool _multiline;
final bool _caseSensitive;
const ConstRegExp(String source, {bool multiline = false, bool caseSensitive = true, /* etc */})
: _source = source, _multiline = multiline, _caseSensitive = caseSensitive;
RegExp get _regExp => _cache[this] ??= RegExp(_source, multiline: _multiline, caseSensitive: _caseSensitive);
Iterable<RegExpMatch> allMatches(String input, [int start = 0]) => _regExp.allMatches(input, start);
RegExpMatch? matchAsPrefix(String input, [int start = 0]) => _regExp.matchAsPrefix(input, start);
} Then you can do: String foo([Pattern pattern = const ConstRegExp(r"a*b*")]) {
..... someString.firstMatch(pattern) ...
} and get the effect of a |
What about something like the |
It's definitely possible to do It's just that the only use for it is to make constant regexps, and ... constant regexps have not been a big need. In practice, a Allowing you to inline a regexp, like the static RegExp? _re;
...
someFunction() {
...
scanner.scan(_re ??= RegExp(r"..."));
...
} (But then you might as well just use the |
Maybe something like
My only problem with that is the syntax feels a *tad* verbose to me, having used other languages where RegExps are delimited by slashes. It doesn't make that much of a difference in the end, though, I guess. |
If verbosity is the problem, then I give you: extension RegExpShorthand on String {
RegExp get re => RegExp(this);
RegExp get i => RegExp(this, caseSensitive: false);
RegExp get m => RegExp(this, multiLine: true);
RegExp get u => RegExp(this, unicode: true);
RegExp get d => RegExp(this, dotAll: true);
RegExp get im => RegExp(this, caseSensitive: false, multiLine: true);
RegExp get iu => RegExp(this, caseSensitive: false, unicode: true);
RegExp get id => RegExp(this, caseSensitive: false, dotAll: true);
RegExp get mu => RegExp(this, multiLine: true, unicode: true);
RegExp get md => RegExp(this, multiLine: true, dotAll: true);
RegExp get ud => RegExp(this, unicode: true, dotAll: true);
RegExp get imu => RegExp(this, caseSensitive: false, multiLine: true, unicode: true);
RegExp get imd => RegExp(this, caseSensitive: false, unicode: true, dotAll: true);
RegExp get iud => RegExp(this, caseSensitive: false, unicode: true, dotAll: true);
RegExp get mud => RegExp(this, multiLine: true, unicode: true, dotAll: true);
RegExp get imud => RegExp(this, caseSensitive: false, multiLine: true, unicode: true, dotAll: true);
}
void main() {
print("a+b+a+".i.hasMatch("ABBA"));
} Then you can make a RegExp simply as Or make a |
This meta-issue collects a potpourri of small Dart language enhancements that will thrill users and not cost too much to implement.
We want to choose a subset of these to implement in the next quarter(s).
This issue is for discussion of which of these features offer the most bang-per-buck, and whether there are other low-hanging fruity features to add.
To qualify, the feature should be well-defined enough that it doesn't require significant further design work (so either very trivial or already designed to a large degree), it should be localized enough that it's unlikely to interfere with other language features, and it should not involve the more complicated parts of the Dart semantics (like the type system). If it can be implemented entirely in the front-end (aka, it's syntactic sugar), then that's a great advantage.
The initial features here are mainly syntactical niceties (syntactic sugar) and features which are largely self-contained.
Go vote for your favorite feature by +1'ing its issue.
library;
declaration (#1073)import
/export
URIs (#649)await
(#25)&&=
and||=
assignment operators (#122)>>>
operator (#120)(See issues sorted by up-votes).
A rough summary of these features is included below.
Unnamed Library Declarations (#1073)
Allow
library;
to be a library declaration with an empty name.This is equivalent to no library declaration, but it provides a place to hang library annotations. Currently you have to give a library a name in order to, say, deprecate it, which seems (and really is) unnecessary.
Can also, over time, be used to move library dartdoc away from the import section.
Import/Export Shorthand Syntax (#649)
Allow import shorthands like:
We'll allow a sequence of some non-space, non-
;
characters (potentially only.
,/
,:
, letters and digits) to act as a a shorthand for the full URI. We still accept the full URI for cases that contain other characters.There might be some third-party tools which parse imports using RegExps. If so, those will need to change.
Parenthesized Element Expressions (#780)
Allow
as an elementExpression. That allows
which is otherwise not expressible to express without introducing artificial intermediates because the
else
would bind to the nearestif
.If parsing to an AST, grouping parentheses can be ignored, but the formatter needs to be aware of them.
Null Aware Element Expressions (#323)
Allow
? expression
as an element expression, so[?x]
is equivalent to[if (x != null) x]
, but only evaluatingx
once.This is a sweet spot of null-awareness, without too much complexity, and it fits well with
…?
.Unparenthesized Function Parameter (#320)
Allow
(foo) => expr
to be written asfoo => expr
. It only applies to a single untyped argument of an arrow-syntax function expression.Might not be worth it compared to just having
=> expr
have an implicit parameter of(it)
.Named Arguments Everywhere (#1072)
Allow named arguments to occur before positional arguments in an argument list.
Currently you cannot do
expectAsync(count: 2, () { … })
, but have to put thecount
argument after the large function body where it's harder to find.By allowing named arguments before positional arguments, this becomes possible. Evaluation order of arguments is still left-to-right, so it cannot simply be desugared away by moving the argument. The front end can introduce
let
constructs to force the order of evaluation and then pass the resulting values in the same order as before.Suffix Await (#25)
Allow
e.await
inasync
functions to mean the same as(await e)
.Since
await
as a reserved word in those functions, this will not conflict with any existing code.It reads much better in long chains of operations because it doesn't need parentheses.
It will also have to work with
e?.await
,e..await
ande?..await
, and generally look like a getter. Maybe only allow it on expressions of typeFutureOr<T>
andFuture<T>
.Can be desugared in the front-end.
&&=
and||=
Short-circuit Assignments (#122)Allow
x &&= y
to meanx ? x = y : false
(aka.x && x = y
) andx ||= y
to meanx ? true : x = y;
(aka.x || x = y
), wherex
is only evaluated once as usual.These can be desugared entirely in the front-end.
Triple Shift (#120)
Implement
>>>
as a user-declarable operator with the same precedence as>>
.This includes allowing
#>>>
andconst Symbol(">>>")
as symbols.(We can then also implement
int.>>>
when possible).Might need minimal backend support as well as front-end changes.
Setter-Named Symbol Literals (#301)
Allow
#foo=
to be a symbol literal for the setter namefoo=
. Must work for private symbols too.Number Digit Separators (#2)
Allow one or more
_
characters between digits in number literals. The_
s have no effect, they are ignored when figuring out the numeric value of the literal. They work for decimal, hexadecimal and floating point literals, in the latter case also in the exponent section, as long as they are flanked by digits on both sides (where hexadecimal literal digits include the lettersa
..f
, and all other literals don't).There is no change to
int.parse
or similar functions. Anyone needing to parse numbers containing_
characters can remove them first usingString.replaceAll
.Can happen entirely in the front-end.
Binary Integer Literals
Allow binary integers similar to hexadecimal integers. That would be:
0b1001
for 9.It's just a new way of writing an integer literal, it can happen entirely in the front-end.
(It's unlikely that we'll want a more general any-radix integer functionality, so this should not conflict with any other feature).
Character Code Constants (#886)
Allow
c"x"
as a way to write 0x79 (it's an integer literal). The string must contain exactly one code point, so it's a constant expression otherwise equal to"x".runes.first
.Can be implemented entirely in the front-end.
Allow generic function types as type arguments (#496);
Currently Dart does not allow a generic function type, like
T Function<T>(T)
to be used as a type argument.This restriction was introduced as a precautionary limit on the type system, but it has been hit by user code, and there is no easy workaround other than using
Function
and having more unsafe code.The issue is amplified by they type inference happily inferring a generic type argument, and then the compiler rejects it immediately after.
We hope that simply removing the check will be sufficient, and that no back-end code is affected.
Generic Metadata (#1297)
Allow const constructor invocations in metadata to have type arguments.
This is currently not allowed by the grammar. There should be on issues with it.
The text was updated successfully, but these errors were encountered: