8000 Should a downwards context of `dynamic` be treated as empty during inference? · Issue #32357 · dart-lang/sdk · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Should a downwards context of dynamic be treated as empty during inference? #32357

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

Closed
leafpetersen opened this issue Feb 28, 2018 · 4 comments
Closed
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang 8000 /language).

Comments

@leafpetersen
Copy link
Member

In discussions here: #32291 it came up that strong mode as implemented in the analyzer treats dynamic in a downwards inference context as an empty context. This is to discuss whether we want this behavior in Dart 2 or not.

cc @lrhn @jmesserly @stereotype441 @munificent

@leafpetersen leafpetersen added the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label Feb 28, 2018
@leafpetersen
Copy link
Member Author

From the original issue, some examples:

T foo<T>(T x) {
  print(T);
  return x;
}
List<T> bar<T>(T x) {
  print(T);
  return [x];
}

List<S> baz<S, T>(Map<S, T> x) {
  print("S = $S, T = $T");
}

void main() {
  var y0 = foo(4);
  dynamic y1 = foo(4);
  Object y2 = foo(4);

  print("Bar");
  var z0 = bar(4);
  List<dynamic> z1 = bar(4);
  List<Object> z2 = bar(4);

  print("[foo]");
  z1 = [foo(4)];
  z2 = [foo(4)];
  List<int> z3 = [foo(4)];

  print("baz");
  List<dynamic> a0 = baz({3: "hello"});
  List<int> a1 = baz({3: "hello"});
  List<num> a2 = baz({3: "hello"});
  List<num> a3 = baz({3: "hello"} ?? {3: "hello"});
}

Currently ddc and ddc -k agree on everything except one place:

int
int
Object
Bar
int
dynamic
Object
[foo]
int // ddc says dynamic, ddc -k says int
Object
int
baz
S = dynamic, T = String
S = int, T = String
S = num, T = String
S = num, T = String

@leafpetersen
Copy link
Member Author

And from @lrhn
What about:

List<T> foo<T>(T x) { print(T); return <T>[x]; }
Object o1 = foo(42);
dynamic o2 = foo(42);
/// And how does FutureOr fit into this?
FutureOr<Object> o3 = foo(42);
FutureOr<dynamic> o3 = foo(42);

@jmesserly
Copy link

Another example is parameters of type dynamic (which can be implicit):

T foo<T>(T x) {
  print('$T $x');
  return x;
}

bar(/*implicit dynamic*/ x, dynamic y, Object z) {}
main() {
  bar(foo(1), foo(2), foo(3));
  // DDC/DDK print:
  // int 1
  // int 2
  // Object 3
}

@munificent
Copy link
Member

We talked about this in person, but I'll capture my thoughts briefly here in case it's helpful. My feeling is that, no, dynamic in a downwards context should not act like an empty context. It should act like a context filled with dynamic.

My arguments are:

  1. Inference always runs the risk of user confusion, so the fewer special rules we have, the better. dynamic is a type, so I don't see any reason for it to act differently than other types with respect to inference. Doing so is just another edge case rule users have to remember.

  2. If we use dynamic to mean empty context, then how do users express when they do want a downwards context filled with dynamic? Will we need to add a reallydynamic syntax?

  3. Conversely, if users do want to express an empty context, there is a natural syntax for that: var. We've trained users already that var means "inference plz thx", so it would be a natural extension to allow List<var>.

@lrhn lrhn closed this as completed Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).
Projects
None yet
Development

No branches or pull requests

4 participants
0