NEWTON

NEWTON


Popular tags

    Cairo: When do you use tempvar and when do you use local

    Asked

    6 months ago

    3

    views


    0

    Since memory cannot be freed, what's the drawback of only using local variables (to avoid revoked refs)? What's the typical use case for tempvars?

    This question was originally posted on Triality

      cairocairo-lang

    2 answers

    0

    From what I have seen, locals are safer to use than tempvars because you will not get your reference revoked as you mentioned above. In most cases you will have the same result using either (I am curious as to the language creators intention). But here is a simple example that could illustrate a slight difference. Take the following to code snippets

    func main{output_ptr : felt*}():

    tempvar x = 1

    return ()

    end

    and

    func main{output_ptr : felt*}():

    alloc_locals

    local x = 1

    return ()

    end

    

    The first code snippet will execute in 3 steps and the second will operate in 4. So I think at least in simple cases for functions you could save operations.

    This answer was originally posted on Triality

    answered

    7 months ago

    0

    One use case of tempvar is with the new operator. You have to use a tempvar otherwise you'll get a The use of 'new' in reference expressions is not allowed error. See: https://cairo-lang.org/docs/how_cairo_works/object_allocation.html?#the-new-operator

    This answer was originally posted on Triality

    answered

    7 months ago

    Your answer

    NEWTON

    NEWTON