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 capturing constructor variables in the "finally" block #48

Open
ggleyzer opened this issue Mar 10, 2023 · 0 comments
Open

Consider capturing constructor variables in the "finally" block #48

ggleyzer opened this issue Mar 10, 2023 · 0 comments

Comments

@ggleyzer
Copy link
Collaborator

Quite often, there is some computation in the constructor that has to be duplicated in the "finally" block. For example, HasherMap constructor has the following:

construct(Hasher<Key> hasher, Map<Key, Value> that)
    {
    if (that.is(HasherMap) && hasher == that.hasher)
        {
        // optimization:
        construct HasherMap(that);
        }
    else
        {
        construct HasherMap(hasher, that.size);
        }
    }
finally
    {
    if (!(that.is(HasherMap) && hasher == that.hasher))
        {
        putAll(that);
        }
    }

With the capture, we could avoid the code duplication:

construct(Hasher<Key> hasher, Map<Key, Value> that)
    {
    Boolean optimized = that.is(HasherMap) && hasher == that.hasher;
    if (optimized)
        {
        construct HasherMap(that);
        }
    else
        {
        construct HasherMap(hasher, that.size);
        }
    }
finally
    {
    if (!optimized)
        {
        putAll(that);
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant