Skip to content

Extension type should be able to have assert for the parameter of the constructor #3354

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

Closed
Cat-sushi opened this issue Sep 17, 2023 · 2 comments
Labels
extension-types feature Proposed language feature that solves one or more problems state-duplicate This issue or pull request already exists

Comments

@Cat-sushi
Copy link

AFAIK, currently extension types doesn't have a way to define assertion for their parameter of their constructors.
As a smart alternative of typedef, assertion seems important.

@Cat-sushi Cat-sushi added the feature Proposed language feature that solves one or more problems label Sep 17, 2023
@Cat-sushi
Copy link
Author

Ex.

extension type NaturalNumber(int value): assert(value > 0){}

@lrhn
Copy link
Member

lrhn commented Sep 17, 2023

Not planned.

You can write a public constructor with an assert:

extension type NaturalNumber._(int value) {
  NaturalNumber(this.value) : assert(value > 0);
}

The declaration header is then only used to declare the representation type and representation object name. You can choose to ignore the private constructor it introduces, and nobody else can see it.

If you really don't want the private constructor to exist (which doesn't change anything, other people can still write -1 as NaturalNumber), that's issue #3343.

@lrhn lrhn closed this as not planned Won't fix, can't repro, duplicate, stale Sep 17, 2023
@lrhn lrhn added state-duplicate This issue or pull request already exists extension-types labels Sep 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-types feature Proposed language feature that solves one or more problems state-duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants