NEWTON

NEWTON


Popular tags

    Creating Elements via a function

    Asked

    4 months ago

    15

    views


    0

    I have a question about creating elements via a function vs doing them directly. Here's a small gist: https://gist.github.com/rlkelly/6cc8f4cce06762c3153fc7e3acaf9429

    %lang starknet
    
    from starkware.cairo.common.alloc import alloc
    
    struct Foo:
        member val: felt
    end
    
    func make_foo(value: felt) -> (foo: Foo):
        let foo = Foo(
            val=value,
        )
        return (foo)
    end
    
    func make_foo_array() -> (entry: Foo*):
        let (struct_array : Foo*) = alloc()
    
        let foo0 = make_foo(0)
        assert struct_array[0] = foo0
        return (struct_array)
    end
    
    @external
    func test_make_foo{syscall_ptr : felt*, range_check_ptr}():
        let (foo_array) = make_foo_array()
        return ()
    end
    
    #   File "starkware/cairo/lang/compiler/preprocessor/preprocessor.py", line 1223, in visit_CodeElementCompoundAssertEq
    #  starkware.cairo.lang.compiler.preprocessor.preprocessor_error.PreprocessorError: contracts/tests/test_foo.cairo:22:5: Cannot compare '__main__.Foo' and '(foo : __main__.Foo)'.
    

    i'm curious why __main__.Foo and (foo : __main__.Foo) are typed differently

    it appears like let foo = and let (foo) =  allocate memory differently

      cairocairo-lang

    Newton

    asked

    4 months ago


    1 answers

    0

    I believe the raw let will evaluate as an expression and the let (foo) will unpack the function return

    https://www.cairo-lang.org/docs/how_cairo_works/functions.html#function-arguments-and-return-values

    answered

    4 months ago

    Your answer

    NEWTON

    NEWTON