10

There is an analyzer/lint check to warn me when it is possible to use a const constructor: https://dart-lang.github.io/linter/lints/prefer_const_constructors.html

(ie. using final a = const A(); instead of final a = A();)

I think to understand the advantages (there will only ever be one instance with the same constant values for a const constructor). But why isn't this the default? Since dart 2 the new can be omitted, so why didn't they change the definition of creating a new instance which can be created const simply as const instead of new? I assume there must be some disadvantage to having everything const?

(for example in a constant context like const [A()] it is actually the same as const [const A()], so why not everywhere)?

Herbert Poul
  • 4,512
  • 2
  • 31
  • 48
  • https://stackoverflow.com/questions/21744677/how-does-the-const-constructor-actually-work – CopsOnRoad Aug 22 '19 at 10:55
  • @CopsOnRoad maybe i'm blind but I still can't find any downsides of using `const` (when it is possible) in the linked question/answers or in the linked blog post? – Herbert Poul Aug 22 '19 at 11:15
  • one of the downside of using `const` is you have to make all the instance variable `final`. – CopsOnRoad Aug 22 '19 at 11:50
  • 1
    @CopsOnRoad this is not a downside of using a `const` instance, just a requirement for a `const` constructor. When I'm using a class which has a `const` constructor (e.g. flutter's `SizedBox`) why would I ever want `SizedBox(height: 8)` (with `new`) instead of `const`? – Herbert Poul Aug 22 '19 at 11:53
  • Give [this](https://stackoverflow.com/a/21746692/6618622) a read. – CopsOnRoad Aug 22 '19 at 12:19
  • I did. But it did not answer my question. To be more specific: Where is the disadvantage of using `const SizedBox(height: 8)` vs. `new SizedBox(height: 8)`? (I think I understand the advantages, but not the disadvantages) – Herbert Poul Aug 22 '19 at 12:25
  • I don't think there is any significant disadvantage of using `const SizedBox(...)` over `new SizedBox(...)`. There are only advantages. `const` is superior than `new` in terms of performance, and except some limitations of `const` like non-final fields not possible etc, there is no disadvantage of using it – CopsOnRoad Aug 22 '19 at 13:08

1 Answers1

12

so why didn't they change the definition of creating a new instance which can be created const simply as const instead of new?

If you mean why doesn't final a = A(); automatically assume const A() if A has a const constructor:

  1. Sometimes it is automatic:

    const a = A();
    

    in which case A's constructor is being invoked in a const context and doesn't need an extra const qualifier on the right-hand-side.

  2. An explicit const expresses intent. For example, suppose you had:

    final a = A(B());
    

    where A and B have const constructors. Later, somebody makes a change:

    final a = A(C());
    

    where C does not have a const constructor. If const were automatic, then you would have no idea that a is no longer const. Maybe that's okay, but it also could suddenly have a negative impact on your application's performance, and without an explicit const qualifier, the impact of a local change could have a much wider scope than expected. (That said, explicit const qualifiers and automatically adding them aren't mutually exclusive.)

  3. const can have downsides. const creates compile-time constants. If you have:

    final a1 = A();
    final a2 = A();
    

    identical(a1, a2) is not true. If const A() were implicit, then identical(a1, a2) would be true, and maybe that's not a property that the code intended to have.

  4. Compile-time constants live forever. The whole point is to have an object that can be reused instead of re-constructing it. The flipside is that won't be destroyed.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • Thanks, i think especially 3&4 make perfect sense. Probably important to keep that in mind when using `Expando`, if `const` objects are never destroyed. Never made that connection, even though it's documented there. – Herbert Poul Aug 23 '19 at 08:00