Created
October 7, 2014 17:16
-
-
Save vojtajina/9d48af8518bc5cd766d8 to your computer and use it in GitHub Desktop.
Dart types in action
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Car { | |
start() {} | |
} | |
class Mustang extends Car { | |
startGasEngine() { | |
print('Starting gas engine.'); | |
} | |
openWindow() {} | |
} | |
class ModelS extends Car { | |
startElectricEngine() { | |
print('Starting electric engine.'); | |
} | |
openWindow() {} | |
} | |
Car getCart(bool x) { | |
if (x) { | |
return new Mustang(); | |
} | |
return new ModelS(); | |
} | |
void addCar(List<Car> cars, Car car) { | |
cars.add(car); | |
} | |
void main(List<String> args) { | |
Car car = getCart(args[0] == 'mustang'); | |
// dartanalyzer: The method 'startGasEngine' is not defined for the class 'Car' | |
// But it will run just fine. | |
car.openWindow(); | |
// dartanalyzer: no issue | |
// Runtime will fail (`dart main.dart mustang`). | |
ModelS m = car; | |
m.startElectricEngine(); | |
// dartanalyzer: no issue | |
// Runtime fail. | |
List<Mustang> cars = []; | |
addCar(cars, new ModelS()); | |
cars[0].startGasEngine(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment