-
Notifications
You must be signed in to change notification settings - Fork 213
Dot syntax for static access #3616
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
Comments
It's worth noting that the example from #357 wouldn't work under this proposal. enum CompassPoint { north, south, east, west }
if (myValue == .north) {} // this should throw an error,
// since the other side of the == operator could have any object But the additional keyword would make it work: class Foo {
bool operator ==(Object from Foo other) {
...
}
} And without a
|
In my opinion it brings little value, but greatly decreases code readability. You never know, where the dotted member comes from. Consider: SomeWidget(
color: .blue // is that MyAppColor? CupertinoColors? Just Colors? - you never know until you hover over it.
) |
The proposal of this issue would not include this case. It would work for enums, static members, factories & constructors of a given type So, if |
@mateusfccp Thank you for the explanation, I had the wrong implementations in my mind. Yet, the issue is still there.
Though most of the time you don't have ambiguous declarations, I'm just bringing it up so it could be considered. |
Sure, you would have to know whether Personally, I don't see this as a huge disadvantage, and you can always use the full reference. We would probably also have linters to enforce the full or dot syntax, depending on the preference of the team. |
I have added somewhat larger-in-scope design proposal. It has some ideas for how to allow more constants than just on the type itself, but it still cannot reach Flutter's |
This^ design proposal is similar to swift's implicit member expressions, except that for "other operators", swift allows only a chain of postfix operators, which always starts with the |
The postfix operator allows operations like I'd probably not allow looking in subclasses or related classes if we also allow postfix expressions. That would open for too many combinations, and increase the risk of conflicts. |
I think this kind of treatment simplifies design and simultaneously expands the scope of the feature. Suppose Foo is a context type. When the user, say, types Here's the benefit: class Colors, instead of being declared as a class, may become (*) currently, class Colors defines a constructor Edit: some commenters on this and other designs are concerned about readability. Making a program readable is the responsibility of the user, not of the language. There're many ways to make the program unreadable with or without this feature. |
Proposal
If a type
T
can be inferred, then interpret.foo
asT.foo
.This is similar to #357, but with a broader use case: it applies to
enum
s,static
members, and also factories & constructors.Example
Here's everything put together:
Note
Setting the
margin
andpadding
would probably need something like flutter/flutter#157890 in order to work.Later On
Static Analysis
If/when this is implemented, it should probably come with some additional linter rules:
from
keywordI really liked the keyword idea from #1955 and would love to see it implemented at some point down the line.
In that issue,
from
was added after the parameter name. I think that attaching it to the type would be best but would be happy either way.The text was updated successfully, but these errors were encountered: