lenscas / tealr

A wrapper around mlua to generate documentation, definition files and other helpers
69 stars 9 forks source link

Support for arrayrecords #61

Open makspll opened 2 years ago

makspll commented 2 years ago

I don't think there's currently a way to support teal arrayrecords

lenscas commented 2 years ago

teal doesn't have array records yet.

Once it does, I want numeric field names to map to them. The only problem is that Rust is 0 based and Lua is 1 based when it comes to indexing. Something that the FromTloLua macro would need to account for.

makspll commented 2 years ago

Hmm are you sure?

I added the "{T}" bit to my "LuaVec" which allowed me to index the vector with integers

lenscas commented 2 years ago

oh, we are talking about different things then. I was talking about https://github.com/teal-language/tl/issues/497

if I remember correctly, a {T} is a table with numeric keys containing 0 or more T's. Tealr should already convert Vec's/Arrays/etc into this.

A {T,T} also known as a tuple, which gets represented as a table with up to 2 numeric keys, containing a value of the type that they correspond do (so, in a {T1,T2} the index 1 will be of type T1 and index 2 will be a T2). I don't think that there is currently a build in way to create these.

makspll commented 2 years ago

This appears in the teal-lang docs, and is what I am talking about:

-- an arrayrecord: a record which doubles as a record and an array
local record TreeNode<T>
   {TreeNode<T>} -- tealr does not allow adding this bit
   item: T
end

Being able to generate this highlighted bit would allow support for all the other record types I believe. Otherwise I am forced to do things like these:

    local cells = life_state.cells as {boolean}
lenscas commented 2 years ago

ah, I see....

hmm... I need to think about how to best do this

lenscas commented 2 years ago

stupid question but, what happens if you overwrite the index metamethod yourself so it takes an integer?

That should show up in the record that tealr generates. And I think that that would allow teal to use ipairs() to loop over it (if it doesn't then I think that is a bug on teals side or I am missing something in how ipairs works)

makspll commented 2 years ago

The record is this right now:

    record LuaVec<T>
        userdata

        -- Mutating methods
        push: function(LuaVec<T>,any):()

        pop: function(LuaVec<T>):(any)

        clear: function(LuaVec<T>):()

        insert: function(LuaVec<T>,(integer),(any)):()

        remove: function(LuaVec<T>,integer):(any)

        -- Meta methods
        metamethod __tostring: function(LuaVec<T>):(string)

        metamethod __index: function(LuaVec<T>,integer):(ReflectedValue)

        metamethod __pairs: function(LuaVec<T>):(function():((any),(any)))

        metamethod __len: function(LuaVec<T>):(integer)

        -- Mutating MetaMethods
        metamethod __newindex: function(LuaVec<T>,(integer),(any)):()

    end

I don't think these indexing metamethods get taken into account, and I tried with setmetamethod as well.

Ipairs does not work either

lenscas commented 2 years ago

going by the error messages, I am going to say that it is a bug in teal. As __index should be called when indexing into a type of LuaVec as a fallback. So, the fact that you can't even do that is, weird.

makspll commented 2 years ago

Yeah it does feel like it, although I don't think teal ever had support for these in the first place, I tried looking for these in commits but no mention of __index, I'll pop em an issue: https://github.com/teal-language/tl/issues/541