cosmos / cosmos-sdk

:chains: A Framework for Building High Value Public Blockchains :sparkles:
https://cosmos.network/
Apache License 2.0
6.08k stars 3.51k forks source link

[Feature]: seperate mutative from immutable api for Int #18066

Open tac0turtle opened 9 months ago

tac0turtle commented 9 months ago

Summary

There has been a need for more mutable apis in math, they have been getting added to the current struct which could cause confusion for users.

Problem Definition

separate immutable from mutable in math

Proposed Feature

Create a new mutable struct for users to use when needed.

New api to add:

julienrbrt commented 9 months ago

Additionally, we can revert this (https://github.com/cosmos/cosmos-sdk/pull/17803, that hasn't been released and which would fit here. I'll edit your message above.

testinginprod commented 9 months ago

The proposal would be to introduce a new type: type IntMut Int  which can be converted from and to Int with little to no overhead by doing (IntMut)(Int) and vice-versa (Int)(IntMut), then the mutative API would be implemented on top of IntMut, this would reduce the API surface of Int which is already big and support the mutative API in a performant way.

Eg:

type IntMut Int

func (i IntMut) ToInt() Int { return (Int)(i) }

func (i IntMut) Quo/Add/Sub/Etc() { ... } 

func (i Int) ToIntMutUnsafe() IntMut { return (IntMut)(i) }
func (i Int) ToIntMut() IntMut { return NewIntMutFromBigInt(i) } // this NewIntMutFromBigInt would copy the underlying Int.bigInt
julienrbrt commented 8 months ago

FWIW comment from the bot: https://github.com/cosmos/cosmos-sdk/pull/18247#discussion_r1371130502