1
1
# Dart static access shorthand
2
2
3
- Author:
[email protected] <
br >Version: 1.
3 (2024-11-29 )
3
+ Author:
[email protected] <
br >Version: 1.
4 (2025-01-08 )
4
4
5
5
You can write ` .foo ` instead of ` ContextType.foo ` when it makes sense. The rules
6
6
are fairly simple and easy to explain.
@@ -381,38 +381,48 @@ a static declaration or constructor declaration *S* when looked up on *D*.
381
381
382
382
* An expression of the form ` const .id(arguments) ` or ` const .new(arguments) `
383
383
is a constant expression. It's a compile-time error if * S* does not
384
- declare a constant constructor, and if any expression in ` arguments ` ,
385
- which are all in a constant context, is not a constant expression.
384
+ declare a corresponding constant constructor, and if any expression
385
+ in ` arguments ` , which are all in a constant context,
386
+ is not a constant expression.
386
387
* An expression of the form ` .<identifier> ` is a constant expression if
387
- * S* declares a constant getter.
388
+ * S* declares a corresponding static constant getter.
388
389
* An expression of the form ` .<identifier> ` that is not followed by an
389
390
` <argumentPart> ` , is a constant expression if `* S* declares
390
- a static method or constructor, and either type inference has not
391
- added type arguments as a generic function instantiation coercion,
391
+ a static method or constructor with base name ` <identifier> ` ,
392
+ and either type inference has not added type arguments as a
393
+ generic function instantiation coercion to the method,
394
+ or to the target class for a constructor,
392
395
or the added type arguments are constant types.
393
396
_ Static tear-offs are constant. Instantiated static tear-offs
394
- are constant if the inferred type arguments are._
395
- that is not a constant expression.) _
397
+ are constant if the inferred type arguments are. Constructor
398
+ tear-offs of generic classes are always on instantiated classes. _
396
399
* An expression of the form ` .new ` which is not followed by the
397
400
selectors of an ` <argumentPart> ` , is a constant expression if
398
- * S* declares a constant (unnamed) constructor, and either type
399
- inference has not added type arguments as a generic function
400
- instantiation coercion, or the added type arguments are constant types.
401
+ * S* declares an unnamed constructor, and either the target
402
+ class is not generic, or type inference has inferred
403
+ constant type arguments for the target class.
404
+ _ It's unlikely that such a tear-off can occur in a constant
405
+ context and be type-valid for the context type, but
406
+ ` const Object o = .new; ` is technically valid._
401
407
* An expression of the form ` .id<typeArguments> ` not followed by
402
- and ` <arguments> ` selector is a constant expression if the type
403
- argument clauses are all constant type expressions.
404
- * (An expression of the form ` .new ` followed by a ` <typeArguments> ` is
405
- still a compile-time error.)
408
+ an ` <arguments> ` selector is a constant expression if the type
409
+ argument clauses are all constant type expressions, and
410
+ * S* declares a corresponding static function. _ (It's still a
411
+ compile-time error if * S* declares a constructor with the base
412
+ name ` id ` , constructors are not generic.)_
413
+ * _ (An expression of the form ` .new ` followed by a ` <typeArguments> ` is
414
+ still a compile-time error.)_
406
415
* An expression of ` .id(arguments) ` or ` .new(arguments) ` is a
407
416
constant expression if (and only if) it occurs in a constant context,
408
- * S* declares a constant constructor, every expression
417
+ * S* declares a corresponding constant constructor, every expression
409
418
in ` arguments ` (which then occurs in a constant context too)
410
- is a constant expression, and inferred type arguments, if any,
411
- are all constant types.
419
+ is a constant expression, and inferred type arguments to the
420
+ target class, if any, are all constant types.
412
421
* An expression of ` .id(arguments) ` or ` .id<typeArguments>(arguments) `
413
- where * S* declares a getter or static function is never a constant
414
- expression. _ There are no ` static ` functions whose invocation is
415
- constant, the only non-instance function which can be invoked as
422
+ where * S* declares a corresponding getter or static function is
423
+ never a constant expression.
424
+ _ There are no ` static ` functions whose invocation is constant,
425
+ the only non-instance function which can be invoked as
416
426
a constant expression is ` identical ` , which is not inside a static
417
427
namespace._
418
428
@@ -424,7 +434,7 @@ a constant function.
424
434
_ The only ` .id ` selector which can come after a constant expression
425
435
and still be constant is ` String.length ` , and it's very hard to
426
436
make that integer satisfy a context type of ` String ` . The only other
427
- selector which can follow complete constant expression and still be
437
+ selector which can follow a complete constant expression and still be
428
438
constant is the not-null check ` ! ` , which is rarely useful in
429
439
constant expressions._
430
440
@@ -702,6 +712,12 @@ not members of `Future`. Primarily to allow people to return values from
702
712
703
713
## Versions
704
714
715
+ 1.4 (2025-01-08): Update constant rules.
716
+
717
+ * Doesn't require a constant ` .new ` tear-off to be a constant constructor.
718
+ That was a typo and was never intended.
719
+ * Adds more words to constant section.
720
+
705
721
1.3 (2024-11-29): Fix constant pattern, clean-up, and expansion
706
722
of the constant section.
707
723
0 commit comments