Skip to content

Implement direct loop over list of structs #2427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Tracked by #2258
certik opened this issue Nov 18, 2023 · 0 comments
Open
Tracked by #2258

Implement direct loop over list of structs #2427

certik opened this issue Nov 18, 2023 · 0 comments

Comments

@certik
Copy link
Contributor

certik commented Nov 18, 2023

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
@certik certik mentioned this issue Nov 18, 2023
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant