Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why abstract class instantiation isn't runtime error in dart?

In many languages if you try to instantiate abstract class you get compile time error. In Dart however, you get warning while compiling and a runtime exception AbstractClassInstantiationError.

Why is that? Can someone provide an example, where it's reasonable to compile such code?

like image 515
Michał Šrajer Avatar asked Dec 19 '22 09:12

Michał Šrajer


1 Answers

Dart tries to allow you to run your program while you are developing it. That is why many things that are compile time errors in other languages are compile-time warnings and runtime errors in Dart. This includes "x is Foo" where Foo doesn't exist, type-annotating with a non-existing types, and calling constructors of partial (abstract) classes.

In short: because it's not a problem that prevents the program from being compiled (unlike a syntax error that might mean the rest of the file is interpreted wrongly), so there is no reason to stop you from running the code. Only if you hit the branch that actually depends on the problem will your program be stopped.

like image 186
lrn Avatar answered Jan 14 '23 15:01

lrn