Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share Static properties between Classes #4069

Open
iPypeNB opened this issue Sep 1, 2024 · 3 comments
Open

Share Static properties between Classes #4069

iPypeNB opened this issue Sep 1, 2024 · 3 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@iPypeNB
Copy link

iPypeNB commented Sep 1, 2024

In order to organize global constants in a dart project, I am looking to implement the following style, short example:

sealed class ConstColor {
  static const String black = '0x000000';
}


abstract class Constants {
  static const colors = ConstColor;
}

void main() {
  print(Constants.colors.black);
}

This is not possible because when I assign ConstColor to the static variable colors in Constants, colors is actually taking the properties of the Type class. Now, my goal is to continue keeping the constants as static properties so as not to load this data at the time of executing the call to Constants but rather to do so from the application load. What options do you recommend, or what other way is recommended to manage constants in an organized way?

Note: I ask the question here because I have been searching in the documentation, forums, etc. and I have not found an answer.

Thanks.

@iPypeNB iPypeNB added the feature Proposed language feature that solves one or more problems label Sep 1, 2024
@mateusfccp
Copy link
Contributor

mateusfccp commented Sep 1, 2024

One possible alternative is to do this way:

final class ConstantColors {
  const ConstantColors._({
    this.black = '0x000000',
    this.red = '0xFF0000',
  });
  
  const factory ConstantColors() = ConstantColors._;

  final String black;
  final String red;
}

abstract class Constants {
  static const colors = ConstantColors();
}

void main() {
  print(Constants.colors.black);
}

It will be a lot more verbose, but it does what you want. Once we have macros, you may use it to generate the class for you so it scales better.

@lrhn
Copy link
Member

lrhn commented Sep 1, 2024

I think you are asking for nested namespaces.
Dart does not have that.

See #266, #267 and #2272 (just from a quick search for "namespace").

The only way to have an extra namespace inside a class, is to have an object, as @mateusfccp suggests.
That does prevent the names from being accessible as constants.

@iPypeNB
Copy link
Author

iPypeNB commented Sep 4, 2024

Thanks for the reply 😊,

I think I'll wait for the macros or namespaces to come out for now, since the solution @mateusfccp propose seems more verbose and doesn't meet the objective I'm trying to obtain from the Sealed classes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

3 participants