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

API improvements #191

Open
kaber2 opened this issue Nov 30, 2024 · 2 comments
Open

API improvements #191

kaber2 opened this issue Nov 30, 2024 · 2 comments

Comments

@kaber2
Copy link

kaber2 commented Nov 30, 2024

When chaining multiple operations, it is necessary to use a seperate declaration in order to pass the variable to the second and following operation:

x := new(uint256.Int)
x.Mul(a, b).Div(x, c).AddUint64(x, 1)

This could by simplified by adding "in-place" variants of all operations that use the receiver as the first argument, f.i.:

func (z *Int) IDiv(b *Int) *Int {
    return z.Div(z, b)
}

resulting in:

x := new(uint256.Int).Mul(a, b).IDiv(c).IAddUint64(1)

Especially when having multiple of these chained operations, this improves readability by providing a uniform picture and makes it easier to avoid mixing up arguments:

x := new(uint256.Int)
x.Mul(a, b).Div(x, c).AddUint64(x, 1)
y := new(uint256.Int)
y.Div(b, a).Mul(y, c).SubUint64(y, 1)
z := new(uint256.Int)
z.Add(a, b).Div(z, c)

vs.

x := new(uint256.Int).Mul(a, b).IDiv(c).IAddUint64(1)
y := new(uint256.Int).Div(b, a).IMul(c).ISubUint64(1)
z := new(uint256.Int).Add(a, b).IDiv(c)

Are there any objections to such an API extension? Otherwise I'll cook up a patch.

@holiman
Copy link
Owner

holiman commented Dec 6, 2024

I think that might make sense, yes, if you're still interested.

@kaber2
Copy link
Author

kaber2 commented Dec 7, 2024

Great, I'll (slowly) get working on 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

2 participants