Skip to content

Static variable declaration of the form "static var v = X" prevents further assigments to v if the value's type is not assignable to X's type #627

Closed
@DartBot

Description

@DartBot

This issue was originally filed by [email protected]


What steps will reproduce the problem?

  1. Run this test with --component=dartc:
    class A {
      static var b = 1;
    }

main() {
  A.b = -1;
  Expect.equals(-1, A.b);
  A.b = "bar";
  Expect.equals("bar", A.b);
  Map bar = new Map<String, int>();
  A.b = bar;
  Expect.equals(bar, A.b);
}

  1. Observe the errors

What is the expected output? What do you see instead?
Spec says (Ch. 7.7):
A non-fi nal static variable declaration of the form static T v; or the form static T v = e; always induces an implicit static setter function (7.3) with signature
static void set v(T x)
whose execution sets the value of v to the incoming argument x.

Doesn't say the setter's argument type is fixed to the runtime type of e, Dynamic is implied and a value of any type should be possible to use.

What version of the product are you using? On what operating system?
latest Dart version, ubuntu 2.6.32-35-generic

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions