algorand / algorand/pyteal

ABI: Provide 1st class arithmetic support for numeric types

Abierto
#181 0 comentarios 0 reacciones 0 asignados Ver en GitHub
Team Scytale
Lenguaje dominante
Python
Estrellas
288
Forks
138
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

## 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

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.