{"id":77,"date":"2025-12-14T12:39:17","date_gmt":"2025-12-14T12:39:17","guid":{"rendered":"https:\/\/blog.metu.edu.tr\/e260506\/?p=77"},"modified":"2025-12-14T15:03:02","modified_gmt":"2025-12-14T15:03:02","slug":"luau-object-oriented-programming","status":"publish","type":"post","link":"https:\/\/blog.metu.edu.tr\/e260506\/2025\/12\/14\/luau-object-oriented-programming\/","title":{"rendered":"Luau Object Oriented Programming"},"content":{"rendered":"<p>A table in Lua is an object in more than one sense. Like objects, tables have a state. Like objects, tables have an identity (a\u00a0<em>selfness<\/em>) that is independent of their values; specifically, two objects (tables) with the same value are different objects, whereas an object can have different values at different times, but it is always the same object. Like objects, tables have a life cycle that is independent of who created them or where they were created.<\/p>\n<p>Check this video: <a href=\"https:\/\/youtu.be\/g1iKA3lSFms?si=hHTzdD-2AMAPjl_j\" target=\"_blank\" rel=\"noopener\">https:\/\/youtu.be\/g1iKA3lSFms?si=hHTzdD-2AMAPjl_j<\/a><\/p>\n<p>Objects have their own operations. Tables also can have operations:<\/p>\n<pre>    Account = {balance = 0}\n    function Account.withdraw (v)\n      Account.balance = Account.balance - v\n    end\n<\/pre>\n<p>This definition creates a new function and stores it in field\u00a0<code>withdraw<\/code>\u00a0of the\u00a0<code>Account<\/code>\u00a0object. Then, we can call it as<\/p>\n<pre>    Account.withdraw(100.00)\n<\/pre>\n<p>This kind of function is almost what we call a\u00a0<em>method<\/em>. However, the use of the global name\u00a0<code>Account<\/code>\u00a0inside the function is a bad programming practice. First, this function will work only for this particular object. Second, even for this particular object the function will work only as long as the object is stored in that particular global variable; if we change the name of this object,\u00a0<code>withdraw<\/code>\u00a0does not work any more:<\/p>\n<pre>    a = Account; Account = nil\n    a.withdraw(100.00)   -- ERROR!\n<\/pre>\n<p>Such behavior violates the previous principle that objects have independent life cycles.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A table in Lua is an object in more than one sense. Like objects, tables have a state. Like objects, tables have an identity (a\u00a0selfness) that is independent of their values; specifically, two objects (tables) with the same value are different objects, whereas an object can have different values at different times, but it is [&hellip;]<\/p>\n","protected":false},"author":9253,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[8],"tags":[],"class_list":["post-77","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/blog.metu.edu.tr\/e260506\/wp-json\/wp\/v2\/posts\/77","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.metu.edu.tr\/e260506\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.metu.edu.tr\/e260506\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.metu.edu.tr\/e260506\/wp-json\/wp\/v2\/users\/9253"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.metu.edu.tr\/e260506\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":0,"href":"https:\/\/blog.metu.edu.tr\/e260506\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.metu.edu.tr\/e260506\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.metu.edu.tr\/e260506\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.metu.edu.tr\/e260506\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}