File tree Expand file tree Collapse file tree 6 files changed +169
-0
lines changed
tests/language/declaring_constructors Expand file tree Collapse file tree 6 files changed +169
-0
lines changed Original file line number Diff line number Diff line change 1+ // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+ // for details. All rights reserved. Use of this source code is governed by a
3+ // BSD-style license that can be found in the LICENSE file.
4+
5+ // A basic declaring body constructor.
6+
7+ // SharedOptions=--enable-experiment=declaring-constructors
8+
9+ import 'package:expect/expect.dart' ;
10+
11+ class Point {
12+ this (var int x, var int y);
13+ }
14+
15+ class PointFinal {
16+ this (final int x, final int y);
17+ }
18+
19+ void main () {
20+ var p1 = Point (1 , 2 );
21+ Expect .equals (1 , p1.x);
22+ Expect .equals (2 , p1.y);
23+
24+ p1.x = 3 ;
25+ Expect .equals (3 , p1.x);
26+
27+ var p2 = PointFinal (3 , 4 );
28+ Expect .equals (3 , p2.x);
29+ Expect .equals (4 , p2.y);
30+ }
Original file line number Diff line number Diff line change 1+ // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+ // for details. All rights reserved. Use of this source code is governed by a
3+ // BSD-style license that can be found in the LICENSE file.
4+
5+ // Declaring constructors are not enabled in versions before release.
6+
7+ // @dart=3.9
8+
9+ class Point (var int x, var int y);
10+ // ^
11+ // [analyzer] unspecified
12+ // [cfe] unspecified
Original file line number Diff line number Diff line change 1+ // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+ // for details. All rights reserved. Use of this source code is governed by a
3+ // BSD-style license that can be found in the LICENSE file.
4+
5+ // Declaring constructors are not enabled by default.
6+
7+ class Point (var int x, var int y);
8+ // ^
9+ // [analyzer] unspecified
10+ // [cfe] unspecified
Original file line number Diff line number Diff line change 1+ // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+ // for details. All rights reserved. Use of this source code is governed by a
3+ // BSD-style license that can be found in the LICENSE file.
4+
5+ // A class that has a declaring header constructor cannot have any other
6+ // non-redirecting generative constructors.
7+
8+ // SharedOptions=--enable-experiment=declaring-constructors
9+
10+ class C1 (var int x) {
11+ C1 .named (this .x);
12+ // ^
13+ // [analyzer] unspecified
14+ // [cfe] unspecified
15+ }
16+
17+ class C2 (final int x) {
18+ C2 .named (this .x);
19+ // ^
20+ // [analyzer] unspecified
21+ // [cfe] unspecified
22+ }
23+
24+ class C3 (int x) {
25+ C3 .named (int x);
26+ // ^
27+ // [analyzer] unspecified
28+ // [cfe] unspecified
29+ }
30+ class C4 () {
31+ C4 .named (int x);
32+ // ^
33+ // [analyzer] unspecified
34+ // [cfe] unspecified
35+ }
36+
37+ class C5 .named (var int x) {
38+ C5 (this .x);
39+ // ^
40+ // [analyzer] unspecified
41+ // [cfe] unspecified
42+ }
43+
44+ class C6 .named (final int x) {
45+ C6 (this .x);
46+ // ^
47+ // [analyzer] unspecified
48+ // [cfe] unspecified
49+ }
50+
51+ class C7 .named (int x) {
52+ C7 (int x);
53+ // ^
54+ // [analyzer] unspecified
55+ // [cfe] unspecified
56+ }
57+
58+ class C8 .named () {
59+ C8 (int x);
60+ // ^
61+ // [analyzer] unspecified
62+ // [cfe] unspecified
63+ }
Original file line number Diff line number Diff line change 1+ // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+ // for details. All rights reserved. Use of this source code is governed by a
3+ // BSD-style license that can be found in the LICENSE file.
4+
5+ // A class that has a declaring header constructor can have redirecting
6+ // generative constructors and factory constructors.
7+
8+ // SharedOptions=--enable-experiment=declaring-constructors
9+
10+ import 'package:expect/expect.dart' ;
11+
12+ class C1 (final int x) {
13+ C1 .redirecting (int x): this (x);
14+ }
15+
16+ class C2 .named (final int x) {
17+ C2 .redirecting (int x): this .named (x);
18+ }
19+
20+ class C3 (final int x) {
21+ factory C3 .from (int value) => C3 (value * 2 );
22+ }
23+
24+ void main () {
25+ Expect .equals (1 , C1 .redirecting (1 ).x);
26+ Expect .equals (2 , C2 .redirecting (2 ).x);
27+ Expect .equals (6 , C3 .from (3 ).x);
28+ }
Original file line number Diff line number Diff line change 1+ // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+ // for details. All rights reserved. Use of this source code is governed by a
3+ // BSD-style license that can be found in the LICENSE file.
4+
5+ // A basic declaring header constructor.
6+
7+ // SharedOptions=--enable-experiment=declaring-constructors
8+
9+ import 'package:expect/expect.dart' ;
10+
11+ class Point (var int x, var int y);
12+
13+ class PointFinal (final int x, final int y);
14+
15+ void main () {
16+ var p1 = Point (1 , 2 );
17+ Expect .equals (1 , p1.x);
18+ Expect .equals (2 , p1.y);
19+
20+ p1.x = 3 ;
21+ Expect .equals (3 , p1.x);
22+
23+ var p2 = PointFinal (3 , 4 );
24+ Expect .equals (3 , p2.x);
25+ Expect .equals (4 , p2.y);
26+ }
You can’t perform that action at this time.
0 commit comments