在 'let' 中声明一个 'local' 变量是什么意思?
What does it mean to declare a 'local' variable inside a 'let'?
据我了解,let
定义了一个引用,可以将其视为别名,因此例如 let x = y * y * y
不计算 y * y * y
但出现 x
将替换为 y * y * y
。
局部变量类似于其他语言的局部变量。
如https://www.cairo-lang.org/docs/hello_cairo/dict.html,写成let (local dict_start : DictAccess*) = alloc()
是什么意思? local dict_start : DictAccess*
的每个实例都将被 alloc()
替换?为什么不只是 local (dict_start : DictAccess*) = alloc()
或 let (dict_start : DictAccess*) = alloc()
?
首先请注意,当函数被调用时,returned 值被放置在内存中,例如在 return 是单个值的 alloc
的情况下,return 值可以在 [ap-1]
中找到(您可以阅读有关堆栈结构和函数调用的更多信息 here).
let (dict_start : DictAccess*) = alloc()
实际上是有效的,并且是以下语法糖:
alloc()
let dict_start = [ap-1]
let (local dict_start : DictAccess*) = alloc()
等同于:
alloc()
let dict_start = [ap-1]
local dict_start = dict_start
在最后一行,我将dict_start
值引用的值代入一个局部变量,并将引用dict_start
重新绑定到局部变量所在的位置。使用它的动机可能是为了避免潜在的撤销(这可以通过将 return 值放在局部变量中来解决)。这可能是你想用 local (dict_start : DictAccess*) = alloc()
做的,这根本不受当前版本的编译器的支持。
据我了解,let
定义了一个引用,可以将其视为别名,因此例如 let x = y * y * y
不计算 y * y * y
但出现 x
将替换为 y * y * y
。
局部变量类似于其他语言的局部变量。
如https://www.cairo-lang.org/docs/hello_cairo/dict.html,写成let (local dict_start : DictAccess*) = alloc()
是什么意思? local dict_start : DictAccess*
的每个实例都将被 alloc()
替换?为什么不只是 local (dict_start : DictAccess*) = alloc()
或 let (dict_start : DictAccess*) = alloc()
?
首先请注意,当函数被调用时,returned 值被放置在内存中,例如在 return 是单个值的 alloc
的情况下,return 值可以在 [ap-1]
中找到(您可以阅读有关堆栈结构和函数调用的更多信息 here).
let (dict_start : DictAccess*) = alloc()
实际上是有效的,并且是以下语法糖:
alloc()
let dict_start = [ap-1]
let (local dict_start : DictAccess*) = alloc()
等同于:
alloc()
let dict_start = [ap-1]
local dict_start = dict_start
在最后一行,我将dict_start
值引用的值代入一个局部变量,并将引用dict_start
重新绑定到局部变量所在的位置。使用它的动机可能是为了避免潜在的撤销(这可以通过将 return 值放在局部变量中来解决)。这可能是你想用 local (dict_start : DictAccess*) = alloc()
做的,这根本不受当前版本的编译器的支持。