algorand / algorand/pyteal

ABI: Provide 1st class arithmetic support for numeric types

Aperta
#181 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Team Scytale
Lingua principale
Python
Stelle
288
Fork
138
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

## Problem

The ergonomics for arithmetic operations involving numeric ABI types is lacking.
* Numeric ABI types do _not_ expose a 1st class way to perform arithmetic (e.g. add 2 `abi.Uint16`s via `+`).
* Defining numeric constants is verbose due to the API's create-and-set requirement.

Here's an example of how arithmetic looks today with unsigned integers <= 64 bits:

```python
a = abi.Uint16()
b = abi.Uint16()
c = abi.Uint16()
program = Seq(
a.set(100),
b.set(100),
c.set(a.get() + b.get()))
```

Here's a motivational example of how arithmetic and defining numeric constants _can_ look:
```python
a = abi.Uint16()
b = abi.Uint16()
program = Seq(
a.set(100),
b.set(a + abi.Uint16Constant(200)) // No .get() and more concise constant definition
)
```

## Solution

Proposal by @jasonpaulos:
Allow number ABI types to perform math using Python's built-in operators. We will need to introduce a new mini-AST for each numeric type. For example, the addition of two `abi.Uint16`s would need to produce something like a `abi.Uint16BinaryExpr`. And the `abi.Uint16.set` method should accept an `abi.Uint16BinaryExpr` as a possible value, in order to make the expression usable.

For safety, mixing numeric type should probably not be allowed without explicit casting. This issue should also consider implementing a way to cast types, or creating a new issue for it.

Finally, a useful feature might be to allow immediately-creatable numeric constants without having to define a full `abi.Type` -- something like `abi.Uint16Constant` that extends `abi.Uint16` and is only instantiable from a compile-time constant int, for each numeric type.

## Dependencies

#154

## Urgency

TBD

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.