Skip to content

Cannot have a const constructor even if members are const-constructible? #819

Closed
@jamesderlin

Description

@jamesderlin

We'd like to have code like:

class Foo {
  const Foo(this.value);

  final int value;
}

class FooDelegate implements Foo {
  final Foo _foo;

  const FooDelegate(int value) : _foo = Foo(value); // Line 10

  @override
  int get value => _foo.value;
}

but this fails with:

error • Invalid constant value. • const_delegate.dart:10:41 • invalid_constant

We could remove const from the FooDelegate constructor, but without it FooDelegate is no longer perfectly substitutable for Foo (const foo = FooDelegate(1); would fail).

It would be nice if there were some way for a const constructor to const-construct members if possible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhanced-constRequests or proposals about enhanced constant expressionsrequestRequests to resolve a particular developer problemstate-duplicateThis issue or pull request already exists

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions