Closed
Description
Test Tracking: https://dart-current-results.web.app/#/filter=co19/LanguageFeatures/Wildcards
Write up tests for co19. "Possible Test Cases" mirrors the test list for dart-lang/sdk#55652.
Examples pulled from the spec.
Possible Test Cases
May not be exhaustive.
- Multiple
_
are allowed in function parameters. This includes top-level functions, local functions, function expressions ("lambdas"), instance methods, static methods, constructors, etc. It includes all parameter kinds: simple, field formals, and function-typed formals, etc.:
Foo(_, this._, super._, void _(), {_}) {}
list.where((_) => true);
- Multiple
_
are allowed in local variable declaration statement variables. - Multiple
_
are allowed in for loop variable declarations. - Multiple
_
are allowed in catch clause parameters. - Multiple
_
are allowed in generic type and generic function type parameters. - Other declarations can still be named
_
as they are today: top-level variables, top-level function names, type names, member names, etc. are unchanged. -
_
is still a wildcard in patterns
int a;
(_, a) = (1, 2); // Valid.
- Error if we cannot resolve
_
to a member or top-level declaration.
main() {
_ = 1; // Error.
}
class C {
var _;
test() {
_ = 2; // OK.
}
}
- Wildcards do not shadow.
class C {
var _ = 'field';
test() {
var _ = 'local';
_ = 'assign'; // Assigns to the field and not the local.
}
}
- A positional initializing formal named
_
does still initialize a field named_
(and you can still have a field with that name)
class C {
var _;
C(this._); // OK.
}
-
_
can't be accessed inside an initializer list.
class C {
var _;
var other;
C(this._): other = _; // Error, cannot access `this`.
}
- The name
_
can be used in the body, but this is a reference to the field, not the parameter.
class C {
var _;
C(this._) {
print(_); // OK. Prints the field.
}
}
- Error to have two initializing formals named
_
.
class C {
var _;
C(this._, this._); // Error.
}
-
super._
will pass on the given actual argument to the corresponding superconstructor parameter.
class B {
final _;
B(this._);
}
class C extends B {
C(super._); // OK.
}
- An extension type can have a parameter named
_
. This means that the representation variable is named_
, and no formal parameter name is introduced into any scopes.
extension type E(int _) {
int get value => _; // OK, the representation variable name is `_`.
int get sameValue => this._; // OK.
}
- No error when a local declaration
_
isn't used. (Avoid unused variable warnings.) - The name
_
is not introduced into the enclosing scope. - Every kind of declaration named
_
which is specified to be wildcarded is indeed accepted without compile-time errors