Question: Is MetaMethod::IPairs missing for lua54? #636
Answered
by
khvzak
jeremychone
asked this question in
Q&A
|
I think I'm missing something. I'm using the I can do that for Here is an example I would like to create: methods.add_meta_method(MetaMethod::IPairs, |_, _, ()| -> Result<()> {
Err(Error::RuntimeError("attempt to iterate over ...".into()))
});What am I missing? |
Answered by
khvzak
Aug 31, 2025
Replies: 2 comments
|
Lua 5.4 does not support You can use struct MyUserData(i32);
impl LuaUserData for MyUserData {
fn add_methods<M: LuaUserDataMethods<Self>>(methods: &mut M) {
methods.add_meta_method(LuaMetaMethod::Index, |_lua, this, k: i32| {
if k < this.0 {
return Ok(Some(k * k));
}
Ok(None)
});
}
}
lua.load(
r#"
my_ud = ...
for i, x in ipairs(my_ud) do
print(i, x)
end
"#,
)
.call::<()>(MyUserData(5))
.unwrap();or simply provide your own iterator function. If you want to remove |
0 replies
Answer selected by
jeremychone
|
@khvzak Perfect. This is what I was missing. Thank you so much for this precise answer. I have everything I need. Also, thanks for this robust and complete Lua integration. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lua 5.4 does not support
__ipairsmetamethods (it was removed since 5.3).You can use
__indexinstead:or simply provide your own iterator function.
If you want to remove
ipairssupport, then your__indexshould return a error for …