0

i have code in my lua file and i edit that to look like this

function getUserinfo(user_id)
  function call_back_user_info(status , result)
     t = {["first_name"]= result.first_name_, ['have_access']= result.have_access_, ["last_name"]=result.last_name_,["user_name"]=result.username_}
    return t
  end
  getUser(user_id,call_back_user_info)
  end

i need to return t table value when i call getUserinfo function.but it is get me a nil value ! note :getUser function puts data in to call_back_user_info

how i can resolve this problem? thank

  • You need to `return` the `getUser` call. – luther Oct 10 '17 at 18:32
  • Not a solution, but it would seem to be simpler if `getUser` just returned the list `status, result`. So, `return call_back_user_info(getUser(user_id))` or `local status, result = getUser(user_id)`…. – Tom Blodget Oct 11 '17 at 02:40

1 Answers1

1

You can't do a "long return" which returns from an outer function from inside of an inner function.

But what you can do is create a local variable which is closed over, like this:

function getUserinfo(user_id)
  local t

  function call_back_user_info(status , result)
    t = {["first_name"]= result.first_name_,
         ['have_access']= result.have_access_,
         ["last_name"]=result.last_name_,
         ["user_name"]=result.username_}
  end

  getUser(user_id,call_back_user_info)

  return t
end
Tanner Swett
  • 3,241
  • 1
  • 26
  • 32
  • "long `return`" is an interesting play on long jump et al but, as an imported concept, it doesn't quite work—which of course is what you are saying. Lua functions are values and so can be called in any context. A call to the "inner" function isn't necessarily inside a call to the "outer" function. And, in the code from the question, it is apparently inside a call to `getUser` which is inside the call to `getUserinfo`. – Tom Blodget Oct 11 '17 at 02:32