lua-users home
lua-l archive

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


Actually, the instant after I sent that sage I realized that I cannot solve this issue by assigning a dummy value to the second call of SetTitle(), because that will just create another "alias" but not affect the first returned value. I could encapsulate the call in a metatable but that seems pretty elaborate to do what I want.

Michael Newberry

----- Original Message ----- From: "Michael Newberry" <mnewberry@axres.com>
To: "Lua list" <lua@bazar2.conectiva.com.br>
Sent: Wednesday, February 02, 2005 12:14 PM
Subject: Q on functions as first class values


I think I have a found a limitation to the "functions as first class values" design. Can someone please verify the following?

Here is code that calls a C function SetTitle() three times. SetTitle() sets a new string value in C-code and returns the previous one:

   sOldTitle = SetTitle( "String 1" )
   SetTitle( "String 2" )
   SetTitle( sOldTitle )

When SetTitle() is called the 3rd time to restore the title, it sets the title to "String 2", not to sOldTitle, which is what I had intended.

I think I see what is happening (is this correct?): I have assigned sOldTitle as the value of SetTitle(), so the fact that I did not update sOldTitle by the second call to SetTitle() means that sOldTitle was automatically updated by lua_pushstring() inside SetTitle(). Is this correct? If so, then I could solve the problem by using a dummy value to catch the unused return from the second call to SetTitle(). But I wonder if there is a better way; otherwise that is a "gotcha" waiting to happen again.

Thanks,
Michael Newberry