#{...}

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

#{...}

Adrian Sietsma
I note that #{nil,1,2,3} returns 4, in Lua 5.1. This behavior is highly
desirable, but contradicts the 5.1 documentation re #. Can this behaviour be
relied upon (otherwise unpack({...}) would lose embedded nils) ?


Adrian.
Reply | Threaded
Open this post in threaded view
|

Re: #{...}

David Given
Adrian Sietsma wrote:
> I note that #{nil,1,2,3} returns 4, in Lua 5.1. This behavior is highly
> desirable, but contradicts the 5.1 documentation re #. Can this
> behaviour be relied upon (otherwise unpack({...}) would lose embedded
> nils) ?

No; by using an array with a nil in it, you're breaking the rules, and
you're open for all kinds of undefined behaviour.

I believe this also applies to unpack({...}), assuming that ... is
syntactic and not shorthand for 'insert stuff here'. If the ... expands to
a tuple containing a nil, Bad Stuff will happen.

--
+- David Given --McQ-+ "If you're up against someone more intelligent
|  [hidden email]    | than you are, do something insane and let him think
| ([hidden email]) | himself to death." --- Pyanfar Chanur
+- www.cowlark.com --+
Reply | Threaded
Open this post in threaded view
|

Re: #{...}

Adrian Sietsma
David Given wrote:

> Adrian Sietsma wrote:
>
>>I note that #{nil,1,2,3} returns 4, in Lua 5.1. This behavior is highly
>>desirable, but contradicts the 5.1 documentation re #. Can this
>>behaviour be relied upon (otherwise unpack({...}) would lose embedded
>>nils) ?
>
>
> No; by using an array with a nil in it, you're breaking the rules, and
> you're open for all kinds of undefined behaviour.
I know, but this is the special case of a list constructor, not an inserted nil.
>
> I believe this also applies to unpack({...}), assuming that ... is
> syntactic and not shorthand for 'insert stuff here'. If the ... expands to
> a tuple containing a nil, Bad Stuff will happen.
>
That is how i read the docs; however it makes a _huge_ difference; if it can
  be assumed that unpack({...}) == ..., (for both the ... object, or any
arbitrary list) then the following constructs are valid :

function f()
    local t = {a_function_call()}
--do stuff
   return unpack(t)
end

and
function f1(...)
   x.args = {...}
end
function f2()
   f3(unpack(x.args))
end

To be correct according to the docs, I should be doing
unpack(t,1,table.maxn(t)), which is ugly.

So : I like the behaviour as it is; can I rely on it for the life of 5.1 at
least? Otherwise, could unpack() default to maxn ?

Adrian


Reply | Threaded
Open this post in threaded view
|

Re: #{...}

Greg Falcon
On 4/14/06, Adrian Sietsma <[hidden email]> wrote:
> So : I like the behaviour as it is; can I rely on it for the life of 5.1 at
> least? Otherwise, could unpack() default to maxn ?

You are incorrectly assuming the behavior of 5.1 is something other
than it is.  Please be aware that the documented problems of using #
on arrays with nils are not theoretical.  The undefined behavior using
# on arrays with nils exists today.

For instance, I get the following in Lua 5.1:

> =#{nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,1,2,3}
16
> =#{nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,1,2,3}
0

Greg F
Reply | Threaded
Open this post in threaded view
|

Re: #{...}

D Burgess-4
My advice is avoid # on tables.

David B.
Reply | Threaded
Open this post in threaded view
|

Re: #{...}

Mark Hamburg-4
function pack( ... )
    local result = { ... }
    result.n = select( '#', ... )
    return result
end

unpack( t, 1, t.n )

Reply | Threaded
Open this post in threaded view
|

Re: #{...}

Adrian Sietsma
Mark Hamburg wrote:
> function pack( ... )
>     local result = { ... }
>     result.n = select( '#', ... )
>     return result
> end
>
> unpack( t, 1, t.n )
>

I think that is the safest way.

function pack(...)
    return {n=select('#",...),...)
end

Adrian
Reply | Threaded
Open this post in threaded view
|

Re: #{...}

Adrian Sietsma
Adrian Sietsma wrote:
> function pack(...)
>    return {n=select('#",...),...)
> end

or without the typo's


local function pack(...)
    return {n=select('#',...),...}
end

local function unpack_all(t)
    return unpack(t,1,t.n)
end

Adrian