-5

Dart Editor version 1.2.0.release (STABLE). Dart SDK version 1.2.0.

This source code produces runtime exception.

void main() {
  test(new Base());
}

void test(Child child) {
}

class Base {
}

class Child extends Base {
}

I assumed that the analyzer generates something like this.

The argument type 'Base' cannot be assigned to the parameter type 'Child'

But I can only detect this error at runtime when occurred this exception (post factum).

Unhandled exception:
type 'Base' is not a subtype of type 'Child' of 'child'.
mezoni
  • 10,684
  • 4
  • 32
  • 54
  • 1
    This messages makes a lot of sense. You throw Base in a place where you want Child in the arguments name child. Have you tried with a class that doesn't extend/inherit either and get the same message? it's a little cryptic I agree. – Anyone Mar 02 '14 at 10:33

2 Answers2

6

The analyzer is following the language specification here.

It only warns if a the static type of the argument expression is not assignable to the type of function the parameter.

In Dart, expressions of one type is assignable to variables of another type if either type is a subtype of the other.

That is not a safe type check. It does not find all possible errors. On the other hand, it also does not disallow some correct uses like:

Base foo = new Child();
void action(Child c) { ... }
action(foo);  // Perfectly correct code at runtime.

Other languages have safe assignment checks, but they also prevent some correct programs. You then have to add (unsafe/runtime checked) cast operators to tell the compiler that you know the program is safe. It's a trade-off where Dart has chosen to be permissive and avoid most casts.

lrn
  • 64,680
  • 7
  • 105
  • 121
1

Let's try to be polite and answer the question without any prejudice.

I think I understand what you expected and here my angle on what the error means:

  1. You are invoking the method with the argument of type Base
  2. The method is expecting an argument of type Child
  3. The Child is not equal to the Base, neither is a subtype of it (as a fact it is the Child that is a subtype of the Base)

It is working as expected as it makes only sense to provide object of the expected type (or it's subtypes - specialisations).


Update:

After reading again your question I realised that you are pointing out that editor is not finding the type problem. I assume this is due to the point that Dart programs are dynamic and hence certain checks are not done before the runtime.

Hope it helps ;-)

Draško Kokić
  • 1,280
  • 1
  • 19
  • 34
  • I updated my answer as I understood better what your question is. – Draško Kokić Mar 02 '14 at 15:23
  • Are you sure? Dart language specification => Function Invocation => Binding Actuals to Formals => In checked mode, all arguments must belong to subtypes of the type of their corresponding formal -> In checked mode, it is a dynamic type error if oi is not null and the actual type of pi is not a supertype of the type of oi, 1 <= i <= m. It is a dynamic type error if, in checked mode, om+j is not null and the actual type of qj is not a supertype of the type of om+j, 1 <= j <= l. – mezoni Mar 02 '14 at 15:59
  • I got my doubts and read a bit the [doc](https://www.dartlang.org/docs/spec/latest/dart-language-specification.html#h.edxvczd1cw0f) you referred as well a short [explanation](https://www.dartlang.org/dart-tips/dart-tips-ep-2.html) from Seth. It looks that checked/production modes are used in runtime and not necessarily supporting the Dart Editor. – Draško Kokić Mar 02 '14 at 20:15