mlua icon indicating copy to clipboard operation
mlua copied to clipboard

Table macros?

Open nowakf opened this issue 4 years ago • 2 comments

How do you feel about a table macro? Something like:

macro_rules! insert_sequence {
    (@replace_expr $_t:tt $sub:expr) => {$sub};
    (@count $($tts:tt),*) => {0 $(+ insert_sequence!(@replace_expr $tts 1))*};
    ($table:ident => $head:tt $(,)? $($tail:expr),*) => {
        $table.set(insert_sequence!(@count $($tail),*), $head)?;
        insert_sequence!($table => $($tail),*)
    };
    ($t:table =>) => {};
}

So obviously this is pretty rough, and I think procedural macros tend to be more readable, but what do you think about the idea in principle?

I've just been having an fiddly time making tables that have a bunch of different types together, and I think a macro would be a straightforward way to make it more ergonomic. There are two sticky points: you have to pass the lua state, and all the 'set' methods return result, so it's not obvious to me if something as ergonomic as vec![] would be possible.

nowakf avatar Oct 30 '21 23:10 nowakf

Another possibly smarter way to do this might be to implement ToLua for all tuples.

nowakf avatar Oct 30 '21 23:10 nowakf

Sorry, not yet in plans. I'll plan to add macros for userdata.

khvzak avatar Nov 03 '21 23:11 khvzak