dart-sdk/tests/language/constructor_duplicate_final_test.dart
hausner@google.com 6e637eb27f Initializing a final instance is a runtime error
If a final instance variable is initialized at the point of declaration, it
is a runtime error if it is initialized again in a constructor’s initializer
list or by an initializing formal.

If any instance field is initialized more than once in the initializer list of
a constructor, or by an initializing formal parameter, it is a compile time
error.

Adding a new test to cover the expected runtime error.

Fixes issue 13335.

R=regis@google.com

Review URL: https://codereview.chromium.org//505033002

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@39562 260f80e4-7a28-3924-810f-c04153c831b5
2014-08-26 19:37:36 +00:00

25 lines
728 B
Dart

// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Check that duplicate initialization of a final field is a runtime error.
class Class {
final f = 10;
Class(v) : f = v; /// 01: runtime error, static type warning
Class(this.f); /// 02: runtime error, static type warning
// If a field is initialized multiple times in the initializer
// list, it's a compile time error.
Class(this.f) : f = 0; /// 03: compile-time error
}
main() {
new Class(5); /// 01: continued
new Class(5); /// 02: continued
new Class(5); /// 03: continued
}