title: Function returns nil when called inside constructor
body: <p>I just started programming in lua and I created sort of oop structure following this tutorial: <a href="http://tylerneylon.com/a/learn-lua/" rel="nofollow">http://tylerneylon.com/a/learn-lua/</a></p>
  
  <p>Problem is, when I created function that returns object or table of objects and call it inside constructor, it returns nil.</p>
  
  <p>Here is my code for first object:</p>
  
  <pre><code>require "ObjectB"
  
  ObjectA = {}
  
  function ObjectA:new(num)
      newInstance = {}
  
      newInstance.var = self:foo(num)
  
      self.__index = self
      return setmetatable(newInstance, self)
  end
  
  function ObjectA:foo(num)
      return ObjectB:new(num)
  end
  </code></pre>
  
  <p>, and for second object:</p>
  
  <pre><code>ObjectB = {}
  
  function ObjectB:new(num)
      newInstance = {}
  
      newInstance.num = num
  
      self.__index = self
      return setmetatable(newInstance, self)
  end
  </code></pre>
  
  <p>When I do this:</p>
  
  <pre><code>myObject = ObjectA:new(5)
  print(myObject.var.num)
  </code></pre>
  
  <p>, I get error: "Error: main.lua:14: attempt to index field 'var' (a nil value)".
  But when I do this:</p>
  
  <pre><code>myObject = ObjectA:new(5)
  myObject.var = ObjectA:foo(5) //setting var by calling foo outside of constructor
  print(myObject.var.num)
  </code></pre>
  
  <p>, everything seems to work fine and print result is really 5. Can anyone tell me what is reason for this strange behaviour or what am I doing wrong here?</p>