Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for arrayrecords #61

Open
makspll opened this issue Aug 2, 2022 · 9 comments
Open

Support for arrayrecords #61

makspll opened this issue Aug 2, 2022 · 9 comments

Comments

@makspll
Copy link
Contributor

makspll commented Aug 2, 2022

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

@lenscas
Copy link
Owner

lenscas commented Aug 2, 2022

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
Copy link
Contributor Author

makspll commented Aug 2, 2022

Hmm are you sure?

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

@lenscas
Copy link
Owner

lenscas commented Aug 2, 2022

oh, we are talking about different things then. I was talking about teal-language/tl#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
Copy link
Contributor Author

makspll commented Aug 2, 2022

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
Copy link
Owner

lenscas commented Aug 2, 2022

ah, I see....

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

@lenscas
Copy link
Owner

lenscas commented Aug 2, 2022

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
Copy link
Contributor Author

makspll commented Aug 2, 2022

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
Copy link
Owner

lenscas commented Aug 2, 2022

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
Copy link
Contributor Author

makspll commented Aug 2, 2022

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:
teal-language/tl#541

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants