Would assigning result from a function to a variable ruin an opportunity for tail optimization?

StackOverflow https://stackoverflow.com/questions/22243857

  •  10-06-2023
  •  | 
  •  

Domanda

Whenever I have a function to call at the end of another, I always call it on the same line as the return, like so, return foo(x).

Would assigning the result to a variable botch any possibility of tail optimization? Like this,

function(...)
    ...
    tmp = foo(x)
    return tmp
end
È stato utile?

Soluzione

From http://www.lua.org/pil/6.3.html: In Lua, only a call in the format return g(...) is a tail call..

So in this case you are missing the tail call optimization yes.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top