pyteal icon indicating copy to clipboard operation
pyteal copied to clipboard

ABI: Named tuple support

Open jasonpaulos opened this issue 3 years ago • 3 comments
trafficstars

Summary

Python allows custom named tuples to be created with this syntax:

from typing import NamedTuple

class ANamedTuple(NamedTuple):
    """a docstring"""
    foo: int
    bar: str
    baz: list

Since we have abi.Tuple, it is possible we can do something similar and support ABI named tuples in PyTeal.

Scope

A named tuple is essentially a normal tuple, except it has a mapping from user-defined names to its indexes.

Users should be able to define as many named tuples as they want and use them just like they would use a normal abi.Tuple.

Probably we would want a base class called abi.NamedTuple, which user-defined named tuples would subclass, just like the Python example.

I have not yet thought about how abi.NamedTuple would discover the fields on it and map those to tuple indexes, but we can look at Python's NamedTuple implementation for inspiration.

jasonpaulos avatar Feb 01 '22 20:02 jasonpaulos

https://github.com/python/cpython/blob/main/Lib/typing.py#L2241-L2324 for reference, this is the implementation in python

barnjamin avatar Feb 01 '22 20:02 barnjamin

Initial attempt at using subclass of Tuple to provide arbitrary struct like interface https://github.com/algorand-devrel/abi-pyteal/blob/main/struct.py#L20-L41

barnjamin avatar Apr 21 '22 11:04 barnjamin

I'd like to see us distinguish this from abi. Since this is an "internal object", I'd rather it was, roughly, pt.NameTuple or even pt.Struct. We should distinguish between a programming model that pyteal gives - "structs" - from how those things are serialized abi.Tuple, just as Go has structs, but there's no names at the ABI level (in function calling conventions).

jannotti avatar Jul 27 '22 17:07 jannotti