Cosmos-sdk: Reconcile DecCoin/s with Coin/s

Created on 16 Jan 2019  Â·  4Comments  Â·  Source: cosmos/cosmos-sdk

Summary

Reconcile the DecCoin and DecCoins APIs with the Coin and Coins APIs for consistency.

e.g.

  • Not allowing negative amounts
  • Not returning zero amounts in plus
  • Consistent constructors and naming conventions
  • Improve godocs & test coverage
  • etc...

For Admin Use

  • [x] Not duplicate issue
  • [x] Appropriate labels applied
  • [x] Appropriate contributors tagged
  • [x] Contributor assigned/self-assigned
tests types

Most helpful comment

How about defining these common interfaces?

type Coin interface {
    SameDenomAs(Coin) bool
    Zero() bool
    GTE(Coin) bool
    LT(Coin) bool
    Equal(Coin) bool
    Plus(Coin) Coin
    Minus(Coin) Coin
    Positive() bool
    Negative() bool
    String() string
}

type CoinSet interface {
    Valid() bool
    Plus(CoinSet) Coin
    Minus(CoinSet) Coin
    SafeMinus(CoinSet) Coin
    AllGT(CoinSet) bool
    AllGTE(CoinSet) bool
    AllLTE(CoinSet) bool
    Zero() bool
    Equal(CoinSet) bool
    Empty() bool
    AllPositive() bool
    AllNotNegative() bool
        AnyNegative() bool
    Sort() CoinSet
    String() string
}

Note that I have removed the Is prefix from all types - IMHO it does not add any value to the methods names.

All 4 comments

What did you have in mind? like testing through an interface? golang is kinda inhibiting for actually combining these two types unfortunately

Yeah I wish we could keep it DRY as possible, but I mean just making sure the APIs are the same (e.g. plus doesn't make sure zero coins are removed).

How about defining these common interfaces?

type Coin interface {
    SameDenomAs(Coin) bool
    Zero() bool
    GTE(Coin) bool
    LT(Coin) bool
    Equal(Coin) bool
    Plus(Coin) Coin
    Minus(Coin) Coin
    Positive() bool
    Negative() bool
    String() string
}

type CoinSet interface {
    Valid() bool
    Plus(CoinSet) Coin
    Minus(CoinSet) Coin
    SafeMinus(CoinSet) Coin
    AllGT(CoinSet) bool
    AllGTE(CoinSet) bool
    AllLTE(CoinSet) bool
    Zero() bool
    Equal(CoinSet) bool
    Empty() bool
    AllPositive() bool
    AllNotNegative() bool
        AnyNegative() bool
    Sort() CoinSet
    String() string
}

Note that I have removed the Is prefix from all types - IMHO it does not add any value to the methods names.

We might need an Amount interface to allow Int/Dec comparisons

Was this page helpful?
0 / 5 - 0 ratings