NEWTON
Asked
10 months ago
14
views
0
As I understand it, let
defines a reference, which can be seen as an alias, so for example let x = y * y * y
doesn't compute y * y * y
but the occurrences of x
will be replaced by y * y * y
.
Local variables are similar to other languages local variables.
As in https://www.cairo-lang.org/docs/hello_cairo/dict.html, what does it mean to write let (local dict_start : DictAccess*) = alloc()
? That every instance of local dict_start : DictAccess*
will be replaced by alloc()
? Why not just local (dict_start : DictAccess*) = alloc()
or let (dict_start : DictAccess*) = alloc()
?
ㅤ This question was originally posted on Stack Overflow
0
First note that when a function is called, the returned values are placed in memory, so e.g. in the case of alloc
which returns a single value, the return value can be found in [ap-1]
(you can read more about the stack structure and function calls here).
let (dict_start : DictAccess*) = alloc()
is actually valid, and is a syntactic sugar for the following:
alloc()
let dict_start = [ap-1]
let (local dict_start : DictAccess*) = alloc()
is equivalent to:
alloc()
let dict_start = [ap-1]
local dict_start = dict_start
In the last line, I substitute the value referenced by dict_start
value into a local variable, and rebind the reference dict_start
to the location of the local variable. The motivation to use this might be to avoid potential revocations (which may be solved by putting the return value in a local variable). This is probably what you want to do with local (dict_start : DictAccess*) = alloc()
, which is simply not supported by the current version of the compiler.
ㅤ This answer was originally posted on Stack Overflow
answered
9 months ago
What is the difference between tempvar/let in Cairo Lang? How to use allow_locals and local?
Cairo OR operator
Cairo Lang / StarkNet: What are Revoked references? What is alloc_locals?
How to use get_fp_and_pc in Cairo Lang?
Does Cairo Lang have any logic operators?
How to make conditional expressions if..then..else in Cairo lang?
Are there situations in which we prefer declaring variables as `local` vs `tempvar`?
What should I use to execute abi.encodePacked from solidity in starknet?
Do you know a function that converts an array of felts (numbers) to a single string in JS? [Cairo Lang]
Is there any way to abi-encode calldata to pass to `account.execute`?
How expensive are bitwise operations (and/or/xor) vs arithmetic in Cairo?
How to get the block hash in Cairo lang?
How to make conditional expressions if..then..else in Cairo lang?
A plan to get better visibility when a transaction got received but not placed into a block?