Page 1 of 2

Sending nested tables over channels

Posted: Mon Jan 23, 2017 4:01 pm
by Dantevg
Maybe this is a stupid question, but why aren't we able to send nested tables over channels? Is it impossible or very hard to make?
Is there a solution to send nested tables over channels? Serializing? Repeated channel sends?

EDIT: I want to send something like this:

Code: Select all

local t = {
  { type = "foo", "data", "more data", "and more", ... },
  { type = "bar", "data", "data 2", "data 3", ... },
  ...
}

Re: Sending nested tables over channels

Posted: Mon Jan 23, 2017 4:28 pm
by Nixola
Either repeated channel sends or serializing; I personally chose the former. I basically send a number (the number of tables the table contains) followed by each table sent separately.

Re: Sending nested tables over channels

Posted: Mon Jan 23, 2017 6:36 pm
by bartbes
It's a trade-off. Full duplication is error-prone, more expensive, and often overkill. If you do want to send nested tables, you can use any kind of serialization that suits your situation. Serialization code that only supports tables without cyclic references can be much simpler and faster than code that does have to support those, for instance.

Re: Sending nested tables over channels

Posted: Tue Jan 24, 2017 2:09 pm
by Dantevg
bartbes wrote:Serialization code that only supports tables without cyclic references can be much simpler and faster than code that does have to support those, for instance.
What are cyclic references? A table referencing itself? Is that even possible in lua?

Re: Sending nested tables over channels

Posted: Tue Jan 24, 2017 2:26 pm
by raidho36
Dantevg wrote:Is that even possible in lua?
Why wouldn't it be?

Code: Select all

local a = { }
local b = { }
a.b = b
b.a = a

Re: Sending nested tables over channels

Posted: Tue Jan 24, 2017 3:16 pm
by Dantevg
raidho36 wrote:Why wouldn't it be?

Code: Select all

local a = { }
local b = { }
a.b = b
b.a = a
Okay, that's simple indeed, now I get it.

Re: Sending nested tables over channels

Posted: Sun Jan 29, 2017 10:25 pm
by Robin
If I may do some shameless self-promotion, you can use bitser to send data over channels. Bitser is both flexible and really fast.

Re: Sending nested tables over channels

Posted: Tue Jan 31, 2017 9:44 am
by Dantevg
Something related: Why do all the channel functions accept flat tables, but the channel.peek() function doesn't?

EDIT: Bitser may be really helpful if I get stuck on this problem

Re: Sending nested tables over channels

Posted: Tue Jan 31, 2017 9:53 am
by zorg
Dantevg wrote:Something related: Why do all the channel functions accept flat tables, but the channel.peek() function doesn't?
Even if the wiki page states this, did you try it yourself? I mean, logically, it should also support them. And if it does, then the article needs to be fixed, it's that simple.

Re: Sending nested tables over channels

Posted: Tue Jan 31, 2017 10:06 am
by bartbes
Dantevg wrote:Something related: Why do all the channel functions accept flat tables, but the channel.peek() function doesn't?
Because peek doesn't send stuff?