Skip to content
Discussion options

You must be logged in to vote

Lua 5.4 does not support __ipairs metamethods (it was removed since 5.3).

You can use __index instead:

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 ipairs support, then your __index should return a error for …

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by jeremychone
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #635 on August 31, 2025 20:24.