| // Copyright (c) 2025, 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. |
| |
| /// @assertion The semantics of the primary constructor is found in the |
| /// following steps, where `D` is the class, mixin class, extension type, or |
| /// enum declaration in the program that includes a primary constructor `k`, and |
| /// `D2` is the result of the derivation of the semantics of `D`. The derivation |
| /// step will delete elements that amount to the primary constructor. |
| /// Semantically, it will add a new constructor `k2`, and it will add zero or |
| /// more instance variable declarations. |
| /// ... |
| /// The formal parameter list `L2` of `k2` is identical to `L`, except that each |
| /// formal parameter is processed as follows. |
| /// ... |
| /// - Otherwise, it is a declaring parameter. A formal parameter (named or |
| /// positional) of the form `var T p` or `final T p` where `T` is a type and |
| /// `p` is an identifier is replaced in `L2` by `this.p`, along with its |
| /// default value, if any. The same is done in the case where the formal |
| /// parameter has the form `var p` or `final p`, and `T` is the declared type |
| /// of `p` which was obtained by inference. If the parameter has the modifier |
| /// `var` and `D` is an extension type declaration then a compile-time error |
| /// occurs. Otherwise, if `D` is not an extension type declaration, a semantic |
| /// instance variable declaration corresponding to the syntax `T p;` or |
| /// `final T p;` is added to `D2`. It includes the modifier `final` if and |
| /// only if the parameter in `L` has the modifier `final` and `D` is not an |
| /// extension type declaration. Otherwise, if `D` is an extension type |
| /// declaration then the name of `p` specifies the name of the representation |
| /// variable. In all cases, if `p` has the modifier `covariant` then this |
| /// modifier is removed from the parameter in `L2`, and it is added to the |
| /// instance variable declaration named `p`. |
| /// |
| /// @description Check that a formal parameter of the form `var T p` and |
| /// `final T p` where `T` is a type and `p` is an identifier is replaced in `L2` |
| /// by `this.p` and a semantic instance variable declaration corresponding to |
| /// the syntax `T p;` or `final T p;` is added to `D2`. Test optional named |
| /// parameters. |
| /// @author sgrekhov22@gmail.com |
| |
| // SharedOptions=--enable-experiment=primary-constructors |
| |
| import '../../Utils/expect.dart'; |
| |
| class C1<T>({var String v1 = "default", final T? v2}); |
| |
| class C2({final String v1 = "default", var int v2 = 0}); |
| |
| extension type ET1<T>({final T? v}); |
| |
| extension type ET2({String v = "default"}); |
| |
| enum E1<T>({final T? v}) { |
| e0<String>(v: "E1"), e1; |
| } |
| |
| enum E2({final String v = "default"}) { |
| e0(v: "E2"), e1; |
| } |
| |
| main() { |
| var c1 = C1<int>(v1: "C1", v2: 2); |
| Expect.equals("C1", c1.v1); |
| Expect.equals(2, c1.v2); |
| c1 = C1<int>(); |
| Expect.equals("default", c1.v1); |
| Expect.isNull(c1.v2); |
| |
| var c2 = C2(v1: "C2", v2: 2); |
| Expect.equals("C2", c2.v1); |
| Expect.equals(2, c2.v2); |
| c2 = C2(); |
| Expect.equals("default", c2.v1); |
| Expect.equals(0, c2.v2); |
| |
| var et1 = ET1<String>(v: "ET1"); |
| Expect.equals("ET1", et1.v); |
| et1 = ET1<String>(); |
| Expect.isNull(et1.v); |
| |
| var et2 = ET2(v: "ET2"); |
| Expect.equals("ET2", et2.v); |
| et2 = ET2(); |
| Expect.equals("default", et2.v); |
| |
| Expect.equals("E1", E1.e0.v); |
| Expect.isNull(E1.e1.v); |
| Expect.equals("E2", E2.e0.v); |
| Expect.equals("default", E2.e1.v); |
| } |