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

Consider making Optional not allocate a new absent per type. #397

Open
lrhn opened this issue Nov 17, 2017 · 0 comments
Open

Consider making Optional not allocate a new absent per type. #397

lrhn opened this issue Nov 17, 2017 · 0 comments

Comments

@lrhn
Copy link
Contributor

lrhn commented Nov 17, 2017

I see code like Optional<String> something = const Optional<String>.absent();. This allocates a new empty optional per type, even though they all behave the same.

Have you considered using a single Optional<Null> for all absent optionals?

Even if you can't change the API at this point, you could still make the above allocate an Optional<Null> instead by writing the constructor as:

const factory Optional.absent() = Optional<Null>._absent;
const Optional._absent() : _value = null;

(A little hack-ish, but it should work :).

Since Optional<Null> is a subtype of any other Optional<X>, you get to reuse the same const instance in all the places it's created without type issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants