Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

'is' check in a trinary operator should do type promotion #327

Closed
kevmoo opened this issue Sep 16, 2015 · 8 comments
Closed

'is' check in a trinary operator should do type promotion #327

kevmoo opened this issue Sep 16, 2015 · 8 comments

Comments

@kevmoo
Copy link
Contributor

kevmoo commented Sep 16, 2015

...er, whenever it's called.

void foo(Event e) {
  var inputEvent = e is InputEvent ? e : null;

  // Getting error "Type checked failed. e (Event) is not of type InputEvent 
  bar(e);
}

void bar(InputEvent e) { }
@vsmenon
Copy link
Contributor

vsmenon commented Oct 5, 2015

@kevmoo Can you recheck this? I'm not seeing any problems as of:

> dartanalyzer --version
dartanalyzer version 1.13.0-dev.3.1

@kevmoo
Copy link
Contributor Author

kevmoo commented Oct 5, 2015

Here's the concrete error I'm getting

Analyzing [lib/stagexl.dart]...
[error] Type check failed: inputEvent (T) is not of type InputEvent (/Users/kevmoo/source/github/stageXL/lib/src/events/event_stream.dart, line 199, col 28)
[error] Type check failed: MockSound.load(url, soundLoadOptions) (Future<Sound>) is not of type Sound (/Users/kevmoo/source/github/stageXL/lib/src/media/implementation/audio_element_sound.dart, line 37, col 16)
[error] Type check failed: MockSound.load(url, soundLoadOptions) (Future<Sound>) is not of type Sound (/Users/kevmoo/source/github/stageXL/lib/src/media/implementation/web_audio_api_sound.dart, line 34, col 14)

For https://github.com/bp74/StageXL at bp74/StageXL@37a4ec9

dartanalyzer --strong lib/stagexl.dart

> dartanalyzer --version
dartanalyzer version 1.13.0-dev.5.0

@jmesserly
Copy link
Contributor

@kevmoo type promotion won't happen here, as spec'd in Dart Language.

What is going on: we know T extends Event and event is a T. However, T could be any subtype of event, including a more specific one. We cannot know for sure that InputEvent is the correct promotion.

I'll add this case to #274, but in the short-to-medium term I don't think we can do anything about this case.

In general, type promotion as currently specified is fairly broken, but it's not clear that it can be fixed. I think we need new syntax like Pattern Matching.

@jmesserly
Copy link
Contributor

@vsmenon @leafpetersen unless we can treat the generic parameter T as if it's Event? I seem to recall we had some issues with that, e.g. Point<T extends num> where T was functioning more like dynamic than num.

@jmesserly jmesserly mentioned this issue Oct 5, 2015
6 tasks
@jmesserly
Copy link
Contributor

Eh, strike my suggestion above, that would break assigning the "promoted" variable to a variable of type T, which would be bad.

@jmesserly
Copy link
Contributor

folding this into #274

@jmesserly
Copy link
Contributor

(specifically, this is an example of the existing case "if the tested type S is not a subtype of T, the test won't promote. You'd have to track a union type.")

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

4 participants