0

I have the following code in Lua: ABC:

test (X)

The test function is implemented in C + +. My problem is this: I need to know what the variable name passed as parameter (in this case X). In C + + only have access to the value of this variable, but I must know her name.

Help please

jpjacobs
  • 9,359
  • 36
  • 45
Saulo Cabral
  • 103
  • 1
  • 7
  • 6
    No you don't need to. Find another, more explicit way. –  Jan 19 '12 at 20:34
  • 2
    C++ variables do not have a name at runtime. – Fred Foo Jan 19 '12 at 20:40
  • Why do you think you need to know the variable name? Explain more of what you're trying to do that you think you need this. – Nicol Bolas Jan 19 '12 at 21:19
  • This is just not possible straight away. You could rewrite your function to take a table though, and use it like `test{X=X}` – jpjacobs Jan 20 '12 at 08:08
  • What do you expect to get as the name if test is called like this `test({})`?, or this `test(3+2)` or `test( fn() )`. In all those cases there is no variable, only a value. – Michael Anderson Jan 20 '12 at 08:38
  • Everyone is saying there is no good reason to do things like this, but I disagree: There are good reasons to want to do this kind of thing, But they're not quite what the OP is asking, they're more like getting a string version of the arguments. This done occasionally in C/C++ using macros. e.g. I like getting nice messages from `assert( x!=y );` that look like "ASSERION FAILED: x!=y at line ...". I suspect in lua you need to resort to eval style constructs, which is not as nice : `assert("x!=y")`. – Michael Anderson Jan 20 '12 at 08:45

3 Answers3

3

Functions are not passed variables; they are passed values. Variables are just locations that store values.

When you say X somewhere in your Lua code, that means to get the value from the variable X (note: it's actually more complicated than that, but I won't get into that here).

So when you say test(X), you're saying, "Get the value from the variable X and pass that value as the first parameter to the function test."

What it seems like you want to do is change the contents of X, right? You want to have the test function modify X in some way. Well, you can't really do that directly in Lua. Nor should you.

See, in Lua, you can return values from functions. And you can return multiple values. Even from C++ code, you can return multiple values. So whatever it is you wanted to store in X can just be returned:

X = test(X)

This way, the caller of the function decides what to do with the value, not the function itself. If the caller wants to modify the variable, that's fine. If the caller wants to stick it somewhere else, that's also fine. Your function should not care one way or the other.

Also, this allows the user to do things like test(5). Here, there is no variable; you just pass a value directly. That's one reason why functions cannot modify the "variable" that is passed; because it doesn't have to be a variable. Only values are passed, so the user could simply pass a literal value rather than one stored in a variable.

In short: you can't do it, and you shouldn't want to.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
1

The correct answer is that Lua doesn't really support this, but there is the debug interface. See this question for the solution you're looking for. If you can't get a call to debug to work directly from C++, then wrap your function call with a Lua function that first extracts the debug results and then calls your C++ function.

Community
  • 1
  • 1
BMitch
  • 231,797
  • 42
  • 475
  • 450
0

If what you're after is a string representation of the argument, then you're kind of stuck in lua.

I'm thinking something like in C:

assert( x==y );

Which generates a nice message on failure. In C this is done through macros. Something like this (untested and probably broken).

#define assert(X) if(!(X)) { printf("ASSERION FAILED: %s\n", #X ); abort(); }

Here #X means the string form of the arguments. In the example above that is "x==y". Note that this is subtly different from a variable name - its just the string used in the parser when expanding the macro.

Unfortunately there's no such corresponding functionality in lua. For my lua testing libraries I end up passing the stringified version as part of the expression, so in lua my code looks something like this:

assert( x==y, "x==y")

There may be ways to make this work as assert("x==y") using some kind of string evaluation and closure mechanism, but it seemed to tricky to be worth doing to me.

EDIT:

While this doesn't appear to be possible in pure lua, there's a patched version that does seem to support macros: http://lua-users.org/wiki/LuaMacros . They even have an example of a nicer assert.

Michael Anderson
  • 70,661
  • 7
  • 134
  • 187