-
Notifications
You must be signed in to change notification settings - Fork 213
Type promotion doesn't work on "is!" #80
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
In the same fashion, the if (d is! Derived) {
return; // or throw
}
// d should be inferred as Derived |
Here are some variations of the class Organism {}
class Animal extends Organism {
void eat(Organism food) {}
void kill() {}
}
class Predator1 extends Animal {
void eat(Organism food) {
assert(food is Animal, "Food must be an animal");
food.kill(); // error: kill not defined (but maybe okay due to production behavior)
}
}
class Predator2a extends Animal {
void eat(Organism food) {
if (food is! Animal) throw ArgumentError("Food must be an animal");
food.kill(); // error: kill not defined
}
}
class Predator2b extends Animal {
void eat(Organism food) {
if (!(food is Animal)) throw ArgumentError("Food must be an animal");
food.kill(); // error: kill not defined
}
}
class Predator3a extends Animal {
void eat(Organism food) {
if (food is! Animal) {
throw ArgumentError("Food must be an animal");
} else {
food.kill(); // error: kill not defined
}
}
}
class Predator3b extends Animal {
void eat(Organism food) {
if (!(food is Animal)) {
throw ArgumentError("Food must be an animal");
} else {
food.kill(); // error: kill not defined
}
}
}
class Predator4a extends Animal {
void eat(Organism food) {
if (food is! Animal) return;
food.kill(); // error: kill not defined
}
}
class Predator4b extends Animal {
void eat(Organism food) {
if (!(food is Animal)) return;
food.kill(); // error: kill not defined
}
} For reference, the following compile fine: class Predator5 extends Animal {
void eat(Organism food) {
if (food is Animal) {
food.kill(); // compiles fine, but sheesh!
} else {
throw ArgumentError("Food must be an animal");
}
}
}
class Predator6 extends Animal {
void eat(Organism food) {
var prey = food as Animal; // compiles fine, only if i like this exception
prey.kill(); // compiles fine
}
} |
I believe that 2a, 3a, and 4a are all planned to work in the NNBD release. 2b, 3b, and 4b are interesting. They may just work out of the box: if not, I think they could be made to work. cc @stereotype441 |
All of the examples here are now handled except the assert case, which I don't believe we want to support. Closing this. |
From: @jamesderlin (dart-lang/sdk#35019):
The following code is accepted without error:
However, if I invert the
d is Derived
check:then I get an error:
(The same thing happens if I try
!(d is Derived)
.)This seems inconsistent. Couldn't we internally invert
is!
and theif
-else
clauses to achieve the same behavior as the first version?(And if anyone is wondering why I don't just always use the first version, there are cases where one path has much more code than the other, and for readability I prefer to have the simpler block first, e.g.:
Of course, in that particular case, it be even nicer if:
worked.)
The text was updated successfully, but these errors were encountered: