Page 2 of 2

Re: Sending nested tables over channels

Posted: Tue Jan 31, 2017 10:40 am
by zorg
bartbes wrote:
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?
That only means that peek doesn't have any arguments to it, which wasn't the question (albeit it was worded badly);
[wiki]Channel:peek[/wiki] The value of the message can be a boolean, string, number or a LÖVE userdata. It returns nil if there's no message in the queue.
This text is missing the "flat table" part, implying to some, that you can't peek (at) a flat table, if it was put into a channel.
I can't test this atm, but as i wrote before, logically, this should be a documentation issue.

Re: Sending nested tables over channels

Posted: Tue Jan 31, 2017 11:12 am
by Dantevg
I know the usage of the channel functions. I had already tried it, of course, because it seems so unlogical. When I try to receive a flat table with channel:peek(), it just doesn't receive it. It really doesn't work.

Re: Sending nested tables over channels

Posted: Tue Jan 31, 2017 11:35 am
by bartbes
That seems unlikely.. are you sure it's not something else? The code used in peek and pop is almost identical.

Re: Sending nested tables over channels

Posted: Tue Jan 31, 2017 1:28 pm
by s-ol
definietely does work:

Code: Select all

a = love.thread.newThread [[
  chan = love.thread.getChannel("test")
  val = chan:demand()
  chan:push(val)
]]
a:start()
chan = love.thread.getChannel("test")
chan:push({ a=1, b=2 })
function love.update()
  print(chan:peek().a, chan:peek().b)
end

Re: Sending nested tables over channels

Posted: Tue Jan 31, 2017 2:25 pm
by Dantevg
That's strange... I thought channel:pop() was describable as:

Code: Select all

data = channel:peek()
channel:pop()
Or am I missing something here? In my code it doesn't work...

EDIT: So, the wiki was wrong, thanks for editing!

Re: Sending nested tables over channels

Posted: Tue Jan 31, 2017 5:48 pm
by zorg
AFAICT the only thing that happened with the wiki was that those redundant descriptions for the Variants were removed from the Channel methods, so... yeah. Idk, s-ol showed a working example for peek; for pop, you need to assign the returned value to something, if you want to use it, it's not like love.graphics.pop that just modifies some internal state; the confusion may have come from that.
In other words, you don't even need to use peek, if you just want to get a value from a channel, use pop.

Re: Sending nested tables over channels

Posted: Wed Feb 01, 2017 8:16 am
by Dantevg
Yes I know that channel:peek() is just channel:pop() but without changing the queue. But I want to make sure I don't receive any messages from myself. Or is that already taken care of by LÖVE? I thought channel:pop() did modify some internal state, the queue.