admin管理员组文章数量:1124168
I am interested in knowing how to code a single assignment variable in Lua, similar to this example in Rust.
fn main() {
println!("Hello, world!");
let x; //---> This is the single assignment variable
let y = 20;
x = 10;
x = 20; //---> This will cause a compiler error
println!("{} + {} = {}",x,y,x+y);
}
I am interested in knowing how to code a single assignment variable in Lua, similar to this example in Rust.
fn main() {
println!("Hello, world!");
let x; //---> This is the single assignment variable
let y = 20;
x = 10;
x = 20; //---> This will cause a compiler error
println!("{} + {} = {}",x,y,x+y);
}
Share
Improve this question
asked yesterday
ggg123ggg123
14411 bronze badges
1 Answer
Reset to default 1In Lua 5.4, a local declaration may contain the <const>
attribute, creating a constant variable.
Attempting to reassign a constant variable throws an error:
local foo <const> = 42
foo = 99 -- error: attempt to assign to const variable 'foo'
Lua 5.4: 3.3.7 – Local Declarations
本文标签: immutabilityHow to code a runtimesingleassignmentvariable in LuaStack Overflow
版权声明:本文标题:immutability - How to code a run-time, single-assignment, variable in Lua? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736616165a1945473.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论