Skip to content

Implement direct loop over list of structs #2427

Open
@certik

Description

@certik

This does not work:

def require(requirement: bool, error_message: str):
    if not requirement:
        raise Exception("ASR verify failed: " + error_message)

def verify(asr: Transactions):
    t: Transaction
    for t in asr.transactions:
        require(len(t.items) >= 2, "len(items) >= 2")
        total = 0
        for item in t.items:
            require(item.account_idx >= 0
                and item.account_idx < len(asr.accounts),
                    "0 <= account_idx < len(accounts)")
            require(item.commodity == "$", 'commodity == "$"')
            total += item.amount
        require(total == 0, "sum(items[:].amount) == 0")

Here are the workarounds that make it work today:

def verify(asr: Transactions):
    i: i32
    for i in range(len(asr.transactions)):
        t: Transaction = asr.transactions[i]
        require(len(t.items) >= 2, "len(items) >= 2")
        total: i64 = i64(0)
        j: i32
        for j in range(len(t.items)):
            item: TransactionItem = t.items[j]
            require(item.account_idx >= i64(0)
                and item.account_idx < i64(len(asr.accounts)),
                    "0 <= account_idx < len(accounts)")
            require(item.commodity == "$", 'commodity == "$"')
            total += item.amount
        require(total == i64(0), "sum(items[:].amount) == 0")

There are several issues:

  • Allowing the loops for t in asr.transactions and for item in t.items
  • Allowing to declare t: Translation without initializing it, since it is initialized in the loop

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions