-
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
Proposal: Option to omit type for accessors when type is known or can be inferred #417
Comments
This is how Swift handles enum literals and const/static values. If a location is expecting a value of a certain type (such as a variable or a parameter where the type is known at compile time), the type identifier can be omitted as it can easily be inferred. That being said, I'm sure there's going to be syntax ambiguities that would prohibit this from being so simple to implement. One example that comes to mind is using this feature in combination with a ternary operator:
The At any rate, this would be a cool feature, but IMO you need to change the title of the issue. It's not clear at all what your proposal is based on the title alone, so it's not going to see much long-term traffic. |
I can see how that could be a problem. Do you know how Swift handles that problem? |
I don't know enough about the exact semantics of either Dart or Swift to answer that. We'll have to wait for a member of the Dart team to comment. If I had to guess, I would say that Swift is much more reliant on whitespace than Dart, so it can do things like mandate there be spaces around operators at the language level. For Dart, omitting the spaces around operators, while discouraged (and usually a linter error), is still valid Dart code, so even if the compiler could use the space between the P.S. The new title is wordier but still pretty vague. My suggestion for the title would be something along the lines of "Option to omit type for accessors when type is known or can be inferred". |
Another example on how this could change code written in flutter BeforeColumn(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
'some text',
textAlign: TextAlign.center,
overflow: TextOverflow.visible,
textDirection: TextDirection.rtl,
),
Align(
alignment: Alignment.center,
child: Padding(
padding: EdgeInsets.only(top: 10),
child: Text('some more text'),
),
)
],
); AfterColumn(
mainAxisAlignment: .spaceBetween,
mainAxisSize: .min,
children: <Widget>[
Text(
'some text',
textAlign: .center,
overflow: .visible,
textDirection: .rtl,
),
Align(
alignment: .center,
child: Padding(
padding: .only(top: 10),
child: Text('some more text'),
),
)
],
); In my opinion it makes for much cleaner and more sensible code. Gone are all the repetitions. |
I think this is tightly related to the issue #357 |
There may be some issues where for example a Align(
alignment: .center
) When the expected type has a different name than the value given there may be some issues:
This feature request may only work if the expected type is the exact same as the type given. Not possiblealign: .center // expects [AlignmentGeometry], recieves [Alignment]
margin: .only(left: 10) // expects [EdgeInsetsGeometry], recieves [EdgeInsets] PossibletextAlign: .center // expects [TextAlign], recieves [TextAlign]
color: .black // expects [Color], recieves [Color] I gues the only solution from here is for flutter to adapt to this restraint, but that isnt very likely. |
Pretty much the only way this feature would work is if the type inference will expect a value of the exact type specified by the context. For the example of In the future, this could be resolved with static extension methods. Doing that, it would be possible to add a static |
Another thing that occurred to me is that this feature could also be used for named/factory constructors:
A bit of a contrived example (and less useful as you could just use |
This is a duplicate of #357. Let's continue the discussion there. |
@lrhn Is there a way to transfer the reactions over as well? This issue got 4 times as many public reactions as that thread did and broke the top 10 in reacted-to issues within a day. |
What i would like to do (example in flutter):
Since a FontWeight is expected, there should be an option to omitt that part and only write the method inside the FontWeight class.
The same goes for enums.
The text was updated successfully, but these errors were encountered: