Skip to content

Allow a const record entry to be assignable to a const #3004

Open
@rubenferreira97

Description

@rubenferreira97
import 'package:flutter/material.dart';

class MyColors {
  static const Color primary = Color(0xFF007AFF);
  static const Color secondary = Color(0xFF6C757D);
}

const primaryClass = MyColors.primary;

const myColors = (
  primary: Color(0xFF007AFF),
  secondary: Color(0xFF6C757D),
);

const myColors2 = myColors; // works https://github.com/dart-lang/language/issues/2337
const primaryRecord = myColors.primary; // Error: Const variables must be initialized with a constant value.

If a record is const, and its entries must be const, could we get a way to assign it to a constant? Unfortunately I think a const return (const Color get primary) would need to be present on the getter method. Could we under the hood create a const instance and replace it inline? This may break some things.

The previous code would generate something like this:

const myColors = (
  primary: const Color(0xFF007AFF);
);

const primaryRecord = const Color(0xFF007AFF); // myColors.primary -> const Color(0xFF007AFF);

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhanced-constRequests or proposals about enhanced constant expressionsrequestRequests to resolve a particular developer problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions