NEWTON

NEWTON


Popular tags

    Cairo: How to reassign Uint256 in a conditional

    Asked

    5 months ago

    25

    views


    0

    I have the following code:

    from starkware.cairo.common.uint256 import Uint256

    

    func main():

    test()

    return()

    end

    

    func test() -> (res : felt):

    alloc_locals

    local a : Uint256 = Uint256(low=1, high=0)

    if 1 == 1:

    local a : Uint256 = Uint256(low=2, high=0)

    end

    return (res = a)

    end

    

    But I keep getting Error: code:20:19: Reference 'a' was revoked.

    return (res = a)

    ^

    What is a good pattern for solving this issue?

    This question was originally posted on Triality

      cairocairo-lang

    1 answers

    0

    You can make the dynamic variable in the conditional a felt and use the following pattern:

    Hopefully there will be some updates to the language in the future though, so this won't be an issue

    from starkware.cairo.common.uint256 import Uint256

    

    func main():

    test()

    return()

    end

    

    func test() -> (res : felt):

    alloc_locals

    local a

    if 1 == 1:

    a = 2

    else:

    a = 3

    end

    local b: Uint256 = Uint256(low=a, high=0)

    return (res = a)

    end

    

    This answer was originally posted on Triality

    answered

    5 months ago

    Your answer

    NEWTON

    NEWTON