language icon indicating copy to clipboard operation
language copied to clipboard

Allow for shorter dot syntax to access enum values

Open rami-a opened this issue 5 years ago • 310 comments

When using enums in Dart, it can become tedious to have to specify the full enum name every time. Since Dart has the ability to infer the type, it would be nice to allow the use of shorter dot syntax in a similar manner to Swift

The current way to use enums:

enum CompassPoint {
  north,
  south,
  east,
  west,
}

if (myValue == CompassPoint.north) {
  // do something
}

The proposed alternative:

enum CompassPoint {
  north,
  south,
  east,
  west,
}

if (myValue == .north) {
  // do something
}

rami-a avatar May 16 '19 17:05 rami-a

This would be especially nice in collections:

const supportedDirections = <CompassPoint>{.north, .east, .west};
bool isSupported = supportedDirections.containsAll({.north, .east});

It's worth noting too that we would only allow it in places where we can infer the enum type.

So

final north = .north; // Invalid.
final CompassPoint north = .north; // Valid.
final north = CompassPoint.north; // Valid.

johnsonmh avatar May 16 '19 20:05 johnsonmh

In Swift this feature works not only for enums but also for static properties of classes. See also:

https://github.com/munificent/ui-as-code/issues/7

class Fruit {
    static var apple = Fruit(name: "apple");
    static var banana = Fruit(name: "banana");
    
    var name: String;
    
    init(name: String) {
        self.name = name;
    }
}

func printFruit(fruit: Fruit) {
    print(fruit.name);
}

// .banana is here inferred as Fruit.banana
printFruit(fruit: .banana);

kasperpeulen avatar May 18 '19 07:05 kasperpeulen

How would the resolution work?

If I write .north, then the compiler has to look for all enums that are available (say, any where the name of the enum resolves to the enum class), and if it finds exactly one such which has a north element, use that. If there is more than one enum class in scope with a north element, it's a compile-time error. If there is zero, it is a compile-time error.

If we have a context type, we can use that as a conflict resolution: <CompassPoint>[.north, .south] would prefer CompassPoint.north, CompassPoint,south over any other enum with a north or south element. We won't always have a context type, the example if (myValue == .north) { does not.

Alternatively, we could only allow the short syntax when there is a useful context type. For the equality, you will have to write CompassPoint.north (unless we introduce something like "context type hints" because we know that if one operand of an == operator is a CompassPoint enum type, and enums don't override Object.==, then the other is probably also a CompassPoint, but that's a different can of worms). Then we could extend the behavior to any static constant value of the type it's embedded in. That is, if you have Foo x = .bar; then we check whether Foo has a static constant variable named bar of type Foo, and if so, we use it. That way, a user-written enum class gets the same affordances as a language enum.

I guess we can do that for the non-context type version too, effectively treating any self-typed static constant variable as a potential target for .id.

(Even more alternatively, we can omit the . and just write north. If that name is not in scope, and it's not defined on the interface of this. then we do "magical constant lookup" for enum or enum-like constant declarations in scope. That's a little more dangerous because it might happen by accident.

lrhn avatar May 20 '19 06:05 lrhn

One approach that could be used to avoid writing CompassPoint several times is a local import (#267).

eernstg avatar May 20 '19 08:05 eernstg

How would the resolution work?

@lrhn You may want to study how it works in Swift. I think their implementation is fine.

kasperpeulen avatar May 21 '19 18:05 kasperpeulen

@lrhn

Alternatively, we could only allow the short syntax when there is a useful context type.

If we're taking votes, I vote this ☝️

Regarding the case with if (myValue == .north) {, if myValue is dynamic, then I agree, this should not compile. However; myValue would often already be typed, if it is typed, it should work fine. For example:

void _handleCompassPoint(CompassPoint myValue) {
  if (myValue == .north) {
    // do something
  }   
}

For the equality, you will have to write CompassPoint.north

I don't know enough about this, but I don't see why this would need to be the case if we're going with the "useful context type" only route?

Right now we can do:

final direction = CompassPoint.north;
print(direction == CompassPoint.south); // False.
print(direction == CompassPoint.north); // True.
print("foo" == CompassPoint.north); // False.

If we know that direction is CompassPoint, can we not translate direction == .south to direction == CompassPoint.south? Or is that not how this works?

Even more alternatively, we can omit the . and just write north

I don't personally prefer this approach because we risk collisions with existing in scope variable names. If someone has var foo = 5; and enum Bar { foo, }, and they already have a line foo == 5, we won't know if they mean Bar.foo == 5 or 5 == 5.

johnsonmh avatar May 21 '19 23:05 johnsonmh

The problem with context types is that operator== has an argument type of Object. That gives no useful context type.

We'd have to special case equality with an enum type, so if one operand has an enum type and the other is a shorthand, the shorthand is for an enum value of the other operand's type. That's quite possible, it just doesn't follow from using context types. We have to do something extra for that.

lrhn avatar May 22 '19 11:05 lrhn

We can generalize the concept of "enum value" to any value or factory.

If you use .foo with a context type of T, then check whether the class/mixin declaration of T declares a static foo getter with a type that is a subtype of T. If so, use that as the value. If you do an invocation on .foo, that is .foo<...>(...), then check if the declaration of T declares a constructor or static function with a return type which is a subtype of T. If so, invoke that. For constructors, the context type may even apply type arguments.

It still only works when there is a context type. Otherwise, you have to write the name to give context.

lrhn avatar Jun 24 '19 08:06 lrhn

To omit the . would make sense for widgets with constructors.

From

Text(
  'some text',
  style: FontStyle(
    fontWeight: FontWeight.bold
  ),
),

To

Text(
  'some text',
  style: ( // [FontStyle] omitted
    fontWeight: .bold // [FontWeight] omitted
  ),
),

For enums and widgets without a constructor the . makes sense to keep, but for widgets where the . never existed, it makes sense to not add it.

FontWeight.bold -> .bold // class without a constructor
Overflow.visible -> .visible // enum
color: Color(0xFF000000) -> color: (0xFF000000) // class with constructor

From issue #417

_Some pints may have been presented already

Not include subclasses of type

Invalid
padding: .all(10)

This wont work because the type EdgeInsetsGeometry is expected, but the type EdgeInsets which is a subclass is given.

Valid
textAlign: .cener

This will work because TextAlign is expected and TextAlign is given. The solution for the invalid version would be for flutter to adapt to this constraint.

The ?. issue

Alot of people have pointed out this issue on reddit. The problem is as follows:

bool boldText = true;

textAlign = boldText ? .bold : .normal;

The compiler could interpret this as boldText?.bold. But as mentioned on reddit: https://www.reddit.com/r/FlutterDev/comments/c3prpu/an_option_to_not_write_expected_code_fontweight/ert1nj1?utm_source=share&utm_medium=web2x This will probably not be a problem because the compiler cares about spaces.

Other usecases

void weight(FontWeight fontWeight) {
  // do something
}
weight(.bold);

ReinBentdal avatar Jun 24 '19 16:06 ReinBentdal

@ReinBentdal

Omitting the period for constructors would lead to a whole slew of ambiguous situations simply because parentheses by themselves are meant to signify a grouping of expressions. Ignoring that, though, I think removing the period will make the intent of the code far less clear. (I'm not even sure I'd agree that this concise syntax should be available for default constructors, only for named constructors and factories.)

And about the ?. issue, like I said in both the reddit post and issue #417, the larger issue is not whether the compiler can use whitespace to tell the difference between ?. and ? .. It's what the compiler should do when there isn't any whitespace at all between the two symbols. Take this for example:

int value = isTrue?1:2;

Notice how there is no space between the ? and the 1. It's ugly, but it's valid Dart code. That means the following also needs to be valid code under the new feature:

textAlign = useBold?.bold:.normal;

And now that there's no space between the ? and the ., how should the compiler interpret the ?.? Is it a null-aware accessor? Is it part of the ternary followed by a type-implicit static accessor? This is an ambiguous situation, so a clear behavior needs to be established.

andrewackerman avatar Jun 25 '19 03:06 andrewackerman

A solution could be to introduce a identifyer.

*.bold // example symbol

But then again, that might just bloat the code/ language.

ReinBentdal avatar Jun 25 '19 08:06 ReinBentdal

I'd like to see something along these lines


final example = MyButton("Press Me!", onTap: () => print("foo"));

final example2 = MyButton("Press Me!",
    size: .small, theme: .subtle(), onTap: () => print("foo"));

class MyButton {
  MyButton(
    this.text, {
    @required this.onTap,
    this.icon,
    this.size = .medium,
    this.theme = .standard(),
  });

  final VoidCallback onTap;
  final String text;
  final MyButtonSize size;
  final MyButtonTheme theme;
  final IconData icon;
}

enum MyButtonSize { small, medium, large }

class MyButtonTheme {
  MyButtonTheme.primary()
      : borderColor = Colors.transparent,
        fillColor = Colors.purple,
        textColor = Colors.white,
        iconColor = Colors.white;

  MyButtonTheme.standard()
      : borderColor = Colors.transparent,
        fillColor = Colors.grey,
        textColor = Colors.white,
        iconColor = Colors.white;

  MyButtonTheme.subtle()
      : borderColor = Colors.purple,
        fillColor = Colors.transparent,
        textColor = Colors.purple,
        iconColor = Colors.purple;

  final Color borderColor;
  final Color fillColor;
  final Color textColor;
  final Color iconColor;
}

lukepighetti avatar Feb 27 '20 01:02 lukepighetti

Exhaustive variants and default values are both concepts applicable in a lot of scenarios, and this feature would help in all of them to make the code more readable. I'd love to be able to use this in Flutter!

return Column(
  mainAxisSize: .max,
  mainAxisAlignment: .end,
  crossAxisAlignment: .start,
  children: <Widget>[
    Text('Hello', textAlign: .justify),
    Row(
      crossAxisAlignment: .baseline,
      textBaseline: .alphabetic,
      children: <Widget>[
        Container(color: Colors.red),
        Align(
          alignment: .bottomCenter,
          child: Container(color: Colors.green),
        ),
      ],
    ),
  ],
);

MarcelGarus avatar Jul 03 '20 09:07 MarcelGarus

Replying to @mraleph's comment https://github.com/dart-lang/language/issues/1077#issuecomment-688808539 on this issue since this is the canonical one for enum shorthands:

I think this is extremely simple feature to implement - yet it has a very delightful effect, code becomes less repetetive and easier to read (in certain cases).

I agree that it's delightful when it works. Unfortunately, I don't think it's entirely simple to implement. At least two challenges are I know are:

How does it interact with generics and type inference?

You need a top-down inference context to know what .foo means, but we often use bottom-up inference based on argument types. So in something like:

f<T>(T t) {}

f(.foo)

We don't know what .foo means. This probably tractable by saying, "Sure, if there's no concrete inference context type, you can't use the shorthand", but I worry there are other complications related to this that we haven't realized yet. My experience is that basically anything touching name resolution gets complex.

What does it mean for enum-like classes?

In large part because enums are underpowered in Dart, it's pretty common to turn an enum into an enum-like class so that you can add other members. If this shorthand only works with actual enums, that breaks any existing code that was using the shorthand syntax to access an enum member. I think that would be really painful.

We could try to extend the shorthand to work with enum-like members, but that could get weird. Do we allow it at access any static member defined on the context type? Only static getters whose return type is the surrounding class's type? What if the return type is a subtype?

Or we could make enum types more full-featured so that this transformation isn't needed as often. That's great, but it means the shorthand is tied to a larger feature.

How does it interact with subtyping?

If we extend the shorthand to work with enum-like classes, or make enums more powerful, there's a very good chance you'll have enum or enum-like types that have interesting super- and subtypes. How does the shorthand play with those?

Currently, if I have a function:

foo(int n) {}

I can change the parameter type to accept a wider type:

foo(num n) {}

That's usually not a breaking change, and is a pretty minor, safe thing to do. But if that original parameter was an enum type and people were calling foo with the shorthand syntax, then widening the parameter type might break the context needed to resolve those shorthands. Ouch.

All of this does not mean that I think a shorthand is intractable or a bad idea. Just that it's more complex than it seems and we'll have to put some real thought into doing it right.

munificent avatar Sep 10 '20 18:09 munificent

@munificent

If changing the interface breaks the context to the point that name inference breaks, then that is probably a good thing in the same way that making a breaking change in a package should be statically caught by the compiler. It means that the developer needs to update their code to address the breaking change.

To your last example in particular

foo(int n) {}
// to
foo(num n) {}

if that original parameter was an enum type

Enums don't have a superclass type, so I don't really see how an inheritance issue could arise when dealing with enums. With enum-like classes, maybe, but if you have a function that takes an enum-like value of a specific type, changing the type to a wider superclass type seems like it would be an anti-pattern anyway, and regardless would also fall into what I said earlier about implementing breaking changes resulting in errors in the static analysis of your code being a good thing.

Abion47 avatar Sep 10 '20 19:09 Abion47

Unfortunately, I don't think it's entirely simple to implement. At least two challenges are I know are:

FWIW you list design challenges, not implementation challenges. The feature as I have described it (treat .m as E.m if .m occurs in place where E is statically expected) is in fact extremely simple to implement. You just treat all occurrences of .m as a dynamic, run the whole inference and then at the very end return to .m shorthands - for each of those look at the context type E and check if E.m is assignable to E (this condition might be tightened to require E.m to be specifically static final|const E m). If it is - great, if it is not issue an error. Done. As described it's a feature on the level of complexity of double literals change that we did few years back (double x = 1 is equivalent to double x = 1.0).

I concede that there might be some design challenges here, but I don't think resolving them should be a blocker for releasing "MVP" version of this feature.

Obviously things like grammar ambiguities would need to be ironed out first: but I am not very ambitions here either, I would be totally fine shipping something that only works in parameter positions, lists and on the right hand side of comparisons - which just side steps known ambiguities.

Just that it's more complex than it seems and we'll have to put some real thought into doing it right.

Sometimes putting too much thought into things does not pay off because you are entering the area of diminishing returns (e.g. your design challenges are the great example of things which I think is not worth even thinking about in the context of this language feature) or worse you are entering analysis paralysis which prevents you from moving ahead and actually making the language more delightful to use with simple changes to it.

That's usually not a breaking change, and is a pretty minor, safe thing to do.

You break anybody doing this:

var x = foo;
x = (int n) { /* ... */ }

Does it mean we should maybe unship static tear-offs? Probably not. Same applies to the shorthand syntax being discussed here.

mraleph avatar Sep 10 '20 20:09 mraleph

I'm not a computer scientist but aren't the majority of these issues solved by making it only work with constructors / static fields that share return a type that matches the host class & enum values? That's my only expectation for it anyway, and none of those come through generic types to begin with. If the type is explicit, it seems like the dart tooling would be able to to know what type you're referring to.

I don't think the value of this sugar can be understated. In the context of Flutter it would offer a ton of positive developer experience.

enum FooEnum {
  foo,
  bar,
  baz
}

f(FooEnum t) {}

f(.foo) // tooling sees f(FooEnum .foo)
f(.bar) // tooling sees f(FooEnum .bar)
f(.baz) // tooling sees f(FooEnum .baz)

In the context of Flutter the missing piece that I find first is how to handle foo(Color c) and trying to do foo(.red) for Colors.red. That seems like it would be a nice feature but I'm not sure how you'd handle that quickly and cleanly. I don't think it's necessary to be honest, though.

lukepighetti avatar Sep 10 '20 20:09 lukepighetti

FWIW you list design challenges, not implementation challenges.

Yes, good point. I mispoke there. :)

As described it's a feature on the level of complexity of double literals change that we did few years back

That feature has caused some problems around inference, too, though, for many of the same reasons. Any time you use the surrounding context to know what an expression means while also using the expression to infer the surrounding context, you risk circularity and ambiguity problems. If we ever try to add overloading, this will be painful.

I concede that there might be some design challenges here, but I don't think resolving them should be a blocker for releasing "MVP" version of this feature.

We have been intensely burned on Dart repeatedly by shipping minimum viable features:

  • The cascade syntax is a readability nightmare when used in nested contexts. The language team at the time dismissed this as, "Well, users shouldn't nest it." But they do, all the time, and the code is hard to read because of it. No one correctly understands the precedence and god help you if you try to combine it with a conditional operator.

  • We shipped minimal null-aware operators that were described as a "slam dunk" because of how simple and easy it was. If I recall right, the initial release completely forgot to specify what short-circuiting ??= does. The ?. specified no short-circuiting at all which made it painful and confusing to use in method chains. We are laboriously fixing that now with NNBD and we had to bundle that change into NNBD because it's breaking and needs an explicit migration.

  • The generalized tear-off syntax was basically dead-on-arrival and ended up getting removed.

  • Likewise, the "minimal" type promotion rules initially added to the language didn't cover many common patterns and we are again fixing that with NNBD (even though most of it is not actually related to NNBD) because doing otherwise is a breaking change.

  • The crude syntax-driven exhaustiveness checking for switch statements was maybe sufficient when we were happy with any function possibly silently returning null if it ran past the end without a user realizing but had to be fixed for NNBD.

  • The somewhat-arbitrary set of expressions that are allowed in const is a constant friction point and every couple of releases we end up adding a few more cherry-picked operations to be used there because there is no coherent principle controlling what is and is not allowed in a const expression.

  • The completely arbitrary restriction preventing a method from having both optional positional and optional named parameters causes real pain to users trying to evolve APIs in non-breaking ways.

  • The deliberate simplifications to the original optional type system—mainly covariant everything, no generic methods, and implicit downcasts—were the wrong choice (though made for arguably good reasons at the time) and had to be fixed with an agonizing migration in Dart 2.

I get what you're saying. I'm not arguing that the language team needs to go meditate on a mountain for ten years before we add a single production to the grammar. But I'm pretty certain we have historically been calibrated to underthink language designs to our detriment.

I'm not proposing that we ship a complex feature, I'm suggesting that we think deeply so that we can ship a good simple feature. There are good complex features (null safety) and bad simple ones (non-shorting ?.). Thinking less may by necessity give you a simple feature, but there's no guarantee it will give you a good one.

It's entirely OK if we think through something and decide "We're OK with the feature simply not supporting this case." That's fine. What I want to avoid is shipping it and then realizing "Oh shit, we didn't think about that interaction at all." which has historically happened more than I would like.

You break anybody doing this:

var x = foo;
x = (int n) { /* ... */ }

Does it mean we should maybe unship static tear-offs?

That's why I said "usually". :) I don't think we should unship that, no. But it does factor into the trade-offs of static tear-offs and it is something API maintainers have to think about. The only reason we have been able to change the signature of constructors in the core libraries, which we have done, is because constructors currently can't be torn off.

munificent avatar Sep 10 '20 21:09 munificent

@mraleph wrote:

You just treat all occurrences of .m as a dynamic, run the whole inference and then at the very end return to .m shorthands -

Try that with Enum e = .foo..index.toRadixString();

The index lookup become s dynamic, which means the toRadixString() does too, and it won't notice that you forgot the radix argument. If you go back to .foo, you also need to re-check everything down-stream from there. (Yes, cascades are great for creating problems with context types, because they allow a context type on a receiver.)

We will have to infer the enum target of .foo during inference, just as everything else we do type based. That's probably not a problem, because either you have a context type or you don't, and if you don't, you can't use .foo.

So, for @munificent's design challenges.

  • Using .foo requires an enum type as context type. A type variable is not an enum type. Even though enum declarations are sealed, they have Never as subtype, so T foo<T extends Enum>() => .foo; is not valid. Upwards type inference is not a context type, so it does nothing.

  • For enum-like classes, I'd allow .foo to match any static getter/constant on the context type (which must be an enum, class or mixin type) which returns a subtype of the context type. (That again disqualifies type variable context types because the getter cannot possibly return a subtype of that). I'd even allow .foo(args) if foo is a constructor or static function returning a subtype of the context type.

  • Yes, changing the argument type of a static function might break code, just as going from double to num, or from Function to Object, can today. We'll probably live with that, like we already do.

It's not going to be fun explaining why Set<Enum> s = ...; s.contains(.foo); doesn't work.

I'm more worried about {e? .foo : .bar} being hard to parse.

lrhn avatar Sep 11 '20 11:09 lrhn

Try that with Enum e = .foo..index.toRadixString();

As I have said before: I would be totally fine shipping something that only works in parameter positions, lists and on the right hand side of comparisons - which just side steps known ambiguities.

Instead of going "lets imagine you can use it everywhere" and then finding tons of problems with that it's fine to say "let just allow it in few places and then maybe expand that further".

mraleph avatar Sep 11 '20 11:09 mraleph

@mraleph

As I have said before: I would be totally fine shipping something that only works in parameter positions, lists and on the right hand side of comparisons - which just side steps known ambiguities.

I would not be fine with that. It's too restricted, and not along any clear lines that users can use to understand and remember the limitations. It feels too arbitrary.

A more complete list would probably be to allow .foo in the following positions:

  • Argument list value: bar(.foo, name: .foo)
  • Assignment RHS: x = .foo, Enum x = .foo;
  • Index-operands: e[.foo], e[.foo] = 0
  • Collection literals: [.foo], {.foo}, {.foo: .foo}
  • RHS of ==: e == .foo - based on static type of e.
  • a parenthesized expression or the branches of a conditional expression in those positions.

(It really won't fly if Enum x = .foo; works and Enum x = test ? .foo : .bar; does not. Users going from the former to the latter would be very annoyed at needing to write Enum. in front for no apparent reason. We'll get requests to fix that before we ever ship the feature - but then we hit the hard grammar issue of text?.foo meaning something else. ).

That's basically anywhere we allow an <expression>, plus equality checks.

It's very likely that we won't need binary operators otherwise, class C { operator+(Enum x)=>...} C c = ...; ... c + .foo ..., although the second operand really is an argument, and we'll likely not want it in a receiver position (mostly because enums have almost no useful properties, and because receivers mostly have no context type anyway).

lrhn avatar Sep 11 '20 12:09 lrhn

It really won't fly if Enum x = .foo; works and Enum x = test ? .foo : .bar; does not.

I don't really understand why it has to go all in (especially into complicated territories) to be useful. Yes, users would not be able to write ternaries with shorthands, yes some of them might file feature requests to address this, but if the choice is between (A) not getting the feature at all (because we can't figure out how to resolve grammar ambiguity) and (B) getting it in many places but not in ternaries - I am pretty sure most users would choose (B). What I am advocating here seems to be rather pragmatic.

But in any case I don't think this is my (feature) hill to die on. Ultimately choosing how to scope features is language team's choice, so I trust your collective experience even though I might disagree with the outcome.

mraleph avatar Sep 11 '20 12:09 mraleph

A possible compromise could be: allow to replace enum class name with the keyword "enum". E.g. instead of writing CompassPoint.north write enum.north. It can be resolved according to the rules given by @lrhn in an earlier post:

If I write enum.north, then the compiler has to look for all enums that are available, and if it finds exactly one such which has a north element, use that. If there is more than one enum class in scope with a north element, it's a compile-time error. If there is zero, it is a compile-time error.

Not as short as .north, but doesn't cause any syntax problems - can be used in any context.

Or maybe #.north, though I don't know if # by itself is used for anything right now.

Abion47 avatar Sep 11 '20 17:09 Abion47

I don't know if # by itself is used for anything right now.

It's used by symbol literals. That's another feature that doesn't carry its weight and was arguably under-thought when added to the language.

I wonder how crazy it would be to just use a bare identifier: north. Collisions and shadowing are possible, so you have to decide which name whens when north is both an enum and a local variable, inherited member, etc. But we already have a variety of ways that bare identifiers can be resolved and have to prioritize among them, so adding another may not be the worst idea in the world.

It avoids all grammar problems because there is no new syntax. Of course, it exchanges them for name resolution problems...

munificent avatar Sep 12 '20 00:09 munificent

I don't know if # by itself is used for anything right now.

It's used by symbol literals. That's another feature that doesn't carry its weight and was arguably undert-hought when added to the language.

I wonder how crazy it would be to just use a bare identifier: north. Collisions and shadowing are possible, so you have to decide which name whens when north is both an enum and a local variable, inherited member, etc. But we already have a variety of ways that bare identifiers can be resolved and have to prioritize among them, so adding another may not be the worst idea in the world.

It avoids all grammar problems because there is no new syntax. Of course, it exchanges them for name resolution problems...

I would rather not have that option just because it makes it difficult to know what you're looking at. Just looking at foo: .north vs foo: north, the former is clear that it's shorthand syntax, whereas the latter is unclear if it's shorthand or some variable or getter available in the current scope.

I didn't even know that Dart had distinct Symbol literals, or really even of Symbol as a data type. It sort of makes a certain amount of sense considering Dart's early purpose as a JavaScript alternative, but it's a weird use case and I'm not sure when I would ever use them myself, especially in Flutter.

Abion47 avatar Sep 12 '20 01:09 Abion47

(The Symbol class in Dart is unrelated to JavaScript symbols. Dart uses symbols to represent source names at run-time because it then allows AoT compilers, originally dart2js, to change the names in the compiled program without breaking things that refers to those names. That's used for minification and obfuscation. You only really need symbols when you do reflection, including noSuchMethod and Function.apply. The design is marred, as @munificent hints, by not defining properly whether #_foo.bar and #foo._bar are library private names or not, by allowing you to create symbols at run-time, by not providing a way to create setter-name symbols, and by requiring the constructor to do checking that a const constructor cannot do. And then there is inconsistent use of symbols in dart:mirrors).

lrhn avatar Sep 12 '20 06:09 lrhn

That seems even more than less than unhelpful... and incredibly convoluted.

Abion47 avatar Sep 12 '20 07:09 Abion47

Using just north is probably just as viable, technically, as .north. Design-wise it's fragile, but no more fragile than any unqualified name referring to an import.

If we simply say that an otherwise unresolved identifier, in an enum type context, is resolved against that enum, then it's slightly magical, but it's similar to having an implicitly imported declaration named north. Any other import of the same name would make it stop working, and any local declaration with the same name would shadow it. Here it would then refer to the other imported declaration instead of being a conflict, but that will most likely still give you an error.

If we instead say that enums introduce their item names as top-level names too, only those names always lose in name resolution conflicts, then we get a somewhat similar behavior. If any other declaration in the same scope has the same name, we let it take precedence, because you still have Direction.north as an alternative, And you can always hide the other name. At least for imported enums, that will give roughly the same behavior as just resolving unqualified names to enums, and it doesn't have to use the context type. It would obviously still have a conflict between two different enum items with the same name, but I don't see that as a common issue. For enums declared in the same library, we may or may not want to make the top-level enum name shadow imports. If not, it would be like an identifier resolution rule which says even if you found this one, keep searching, and only if you don't find something better, come back and use this declaration.

This would only work for enums, not any enum-like structure. It will only work if the enum declaration is actually imported. You also need that import to write Direction.north, but technically you don't need the enum to be imported to write .north because it's resolved against the context type, not the lexical scope.

(That then prompts the question of why we didn't just make enum items top-level names to begin with, ... and we did, and then we changed that. I do not remember the reasoning, but it could be because it polluted the namespace. Making it a second-class declaration in resolutions might change that balance).

lrhn avatar Sep 12 '20 07:09 lrhn

We could use local imports, #267, to make the names of enum values available concisely. This allows us to avoid introducing new, "magic" scope rules. The trade-off is that it does not use context types at all, so we can't use foo(one, one, one) to mean foo(MyEnum.one, SomeOtherEnum.one, this.one), which may a pro or a con depending on the beholder. ;-)

eernstg avatar Sep 14 '20 11:09 eernstg

I see three specific approaches mentioned here (one of them only by me, but still ...), so I have tried summarizing them.

Proposal 1: Implicit Enum Expressions

We introduce an expression of the form ``.' `, called an implicit enum expression. This expression must only be used where an enum value "is expected":

  • Where the context type is an enum type. (We may want to finally push the context type into conditional expressions).
  • In case expressions of a switch where the switch expression's static type is an enum type.
  • In ==/!= expressions where the other operand is a non-implicit enum expression (and doens't have one in tail-position) with a static type which is an enum type.

(An enum type is either a type which implements the type of an enum declaration, which means the type itself, or a type variable bounded by it, or a union type (nullable/FutureOr) where the component type is an enum type. A type variable is never an enum type, even if its bound is an enum type.)

If the enum type is T, T?, FutureOr<T>, etc., then .foo is considered equivalent to T.foo.

The .foo expression is likely to be a production of <expression>/<expressionWithoutCascade> itself. That means we need some special-casing around equality expressions to allow <implicitEnumExpression> as at most one of the expressions of an ==/!= expression, and otherwise only allow it as a production of <expression> or <expressionWithoutCascade>.

Pros

This can be extended to enum-like types. We can allow .foo for any context type/switch type/equals type, as long as the type has a static getter named foo returning something of its own type (for switches, it'll have to be constants).

We can extend to invocations: .foo(args) is allowed as well, and can resolve to constructor invocations, or to calls of static methods with a suitable return type.

You can write .foo even if you haven't imported Enum because it's resolved against the type, not the lexical scope.

Cons

Grammar is ambiguous for {e1 ? . foo : e2}. We'll likely make it mean {e1 ? (.foo) : e2} because we already prefer the conditional expression in other situations as well, like {e1 is T ? [e2] : e3} or {e1 as bool ? [e2] : e3}. It's still one more ambiguity.

Proposal 2: Context Resolved Identifiers

Allow a plain identifier foo to be resolved to Enum.foo in the same contexts where .foo would be allowed to work in proposal 1.

Only resolve foo as an enum value if it is not declared in the lexical scope, in the interface of the static type of this, or by any applicable extensions (where this.foo would be an error).

Pros

No new syntax.

Can be extended to enum-like classes and function (constructor) calls, since it is based on context type.

Cons

Doesn't work if foo is in scope or in the current class interface, you'll have to write Enum.foo then (which is not significantly different from having to write this.foo to get the instance member when foo is in the lexical scope).

Proposal 2: Weak Top-Level Names

Make enum Enum {foo, bar, baz} introduce foo, bar and baz as top-level names as well, like a top-level declaration const foo = Enum.foo, bar = Enum.bar, baz = Enum.baz; next to the enum declaration.

To avoid conflicts with these new names, we introduce a notion of weak declarations (strawman name).

In name conflict resolution, a weak declaration is considered less important than a non-weak declaration. Similarly to how we handle conflicts between platform library declarations and user declarations by ignoring the platform library declarations if there is at least one user declaration, we let weak declarations be less important in conflict resolution than non-weak declarations (unless the non-weak declaration is a platform declaration and the weak declaration isn't).

So, priorities become:

  • User normal name
  • User weak name
  • Platform normal name
  • Platform weak name

and name conflict resolution succeeds if there is exactly one declaration for a name which has a higher priority than all the rest.

Pros

No new syntax, no new way to resolve names, just a different (extended) way to resolve conflicts. (This can still break existing code when a user library enum introduces a name conflicting with an existing top-level platform library declaration, say enum Comparisons {different, equal, identical}, where identical would now shadow identical from dart:core.)

Cons

Only works for enums, not for enum-like classes. They will have to declare their top-level names themselves, but then they can't make those names weak declarations. Unless we introduce weak declarations in general, but that seems like a bad feature design. Why only two levels? Which proves that it is an ad-hoc solution, not something which is likely to generalize.

Doesn't extend to constructors.

Proposal 4: Do nothing

If we do nothing special for enums, we can introduce other scope-controlling features that might be useful here too. Like @eernstg suggests, a way to locally import the statics of a class namespace into another scope could make it the user's own responsibility to make shorthand names available, or an enum which wants it done in general can export their static constants in a single line (perhaps static export Enum hide values;)

lrhn avatar Sep 14 '20 11:09 lrhn