Var initialization
Problem
Initializing the scratch var seems weird
program = Seq(
(x := ScratchVar()).store(Int(1)),
x.load()
)
what if?
program = Seq(
v := Var(Int(1)),
v.load()
)
Solution
not even PR'ing this, its maybe even weirder
from pyteal import *
class Var(Expr):
def __init__(self, val: Expr):
self.val = val
self.slot = ScratchSlot()
self.type = val.type_of()
self.has_stored = False
def __str__(self):
if self.has_stored:
return self.slot.load(self.type).__str__()
else:
return self.slot.store(self.val).__str__()
def __teal__(self, options: CompileOptions):
if not self.has_stored:
self.has_stored = True
return self.slot.store(self.val).__teal__(options)
def load(self):
return self.slot.load(self.type)
def store(self, val: Expr):
self.val = val
return self.slot.store(val)
def has_return(self):
return False
def type_of(self):
if self.has_stored:
return self.val.type_of()
else:
return TealType.none
opts = CompileOptions(mode=Mode.Application, version=6)
program = Seq(v := Var(Int(1)), v.store(Int(2)), v.load())
print(compileTeal(program, mode=Mode.Application, version=6))
@jasonpaulos With the introduction of frame pointer, we can probably get started with it, and gradually remove the internal use of scratch slots in current ABI implementation.
What do you think?
Ref: https://github.com/algorand/pyteal/blob/master/pyteal/ast/abi/tuple.py#L75-L76 might benefit from this ticket.
I believe this is almost done in functionality, but we might need a set of public interface for allocating such Var object, and potential implication is https://github.com/algorand/pyteal/issues/560
took a stab at something https://github.com/algorand/pyteal/pull/643