lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


The manual really should say:

"Replaces the value at the given position with the value at the top of the stack and then pops the stack."

This is the usual way stack based languages work, but I would agree that the manual wording implies (or at least allows) your interpretation.

> -----Original Message-----
> From: lua-bounces@bazar2.conectiva.com.br [mailto:lua-
> bounces@bazar2.conectiva.com.br] On Behalf Of Shmuel Zeigerman
> Sent: 25 June 2009 11:11
> To: Lua list
> Subject: Re: lua_replace finding
> 
> Patrick Donnelly wrote:
> > On Thu, Jun 25, 2009 at 12:39 AM, Shmuel Zeigerman<shmuz@013net.net>
> wrote:
> >> When Lua stack has one element, doing lua_replace(L,1) removes that
> element.
> >> (I'd expect the stack to be left unchanged.)
> [...]
> >
> > The manual is pretty explicit about the top element being removed
> from
> > the stack. I don't see why this should be different for this odd use
> > case.
> 
> Because it seems pretty unintuitive, even if it doesn't contradict with
> the manual. From the common sense point of view, replace "a" with "b"
> assumes there should be "b" in place of "a", rather then "nothing".
> 
> And, this use case is not so odd. It is like assignment "var1 = var2",
> where var2 can be anything, var1 included.
> 
> Here is the original use case:
>      if (lua_isnoneornil(L, 1)) {
>          lua_createtable(L, 0, 0);
>          lua_replace(L, 1);
>      }
>      luaL_checktype(L, 1, LUA_TTABLE);
> 
> The code above looked straightforward enough, and when it didn't work
> as
> expected, that was a surprise to me.
> 
> --
> Shmuel