Advertisement
theTANCO

Class.lua.changelog

Mar 8th, 2023 (edited)
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.85 KB | None | 0 0
  1. This is a changelog for this program: https://pastebin.com/t2TvSiSU
  2. Full documentation for this program: https://pastebin.com/upj3fgyZ
  3. A guide for Lua metatables: https://pastebin.com/Kf8LxzE9
  4.  
  5.  
  6. 2024/05/23:
  7. • Removed the local variables 'protectedObject' and 'protectedClass' and moved
  8. the strings to where they were being used because they were only being used
  9. once.
  10.  
  11.  
  12. 2023/12/01:
  13. • Fixed some typos in the documentation.
  14.  
  15.  
  16. 2023/09/09:
  17. • Updated part of the documation to explain why the 'keys' table and for loops
  18. are inside the '__pairs' metamethod.
  19.  
  20.  
  21. 2023/09/08:
  22. • Today I've learned how to use the '__pairs' metamethod, and in doing so I
  23. managed to solve two problems while I was trying to solve only one.
  24. • The pairs metamethod now has a default function that can be overwritten in the
  25. class definition.
  26. • The default function will iterate over both the public and read-only tables.
  27. So now using 'pairs' in a for loop will give you ALL public and read-only
  28. methods and properties.
  29. • The default metamethod is applied within the 'readOnly' function.
  30. • By implementing this addition, it also solved the issue of the lua prompt not
  31. being able to autocomplete read-only data. This was a complete accident, but
  32. very satisfying that I solved two problems at once.
  33. • The documentation and metatables guide have been updated to reflect these
  34. changes and what I've learned today.
  35.  
  36.  
  37. 2023/09/05:
  38. • It seems like the functionality of metatables has changed drastically since I
  39. last tested them with this script, so this update comes with a lot of changes.
  40. • Fortunately these changes shouldn't break anyone's scripts.
  41.  
  42. Part 1: Diagnosing the First Problem
  43. • When using the interactive lua prompt in ComputerCraft, it uses
  44. 'textutils.complete' to autocomplete methods, and as it turns out, that
  45. function doesn't like it if 'getmetatable' doesn't return a table or nil.
  46. • As a result, the changes I made a couple days ago to the class constructor and
  47. read-only function cause the lua prompt to crash. The culprit is at line 1005
  48. of '/rom/apis/textutils.lua'.
  49. • Looking at the code, there's a very simple fix for the API that would avoid
  50. this whole problem, but unfortunately I don't have the power to push that fix
  51. to everyone who might use this script.
  52. • I also noticed with the way I had it before that read-only methods don't show
  53. up in the autocomplete. So I came up with a new idea.
  54.  
  55. Part 2: The Solutions
  56. • The change I've decided to make was to set '__metatable' to false by default.
  57. This allows the textutils API to skip over that problematic line, but the
  58. read-only methods still won't show up in autocomplete.
  59. • What you could do instead is set your own '__metatable' to a "fake" metatable
  60. with an '__index' containing new elements whose keys match the real read-only
  61. '__index'. This makes it so the real metatable can't be changed while allowing
  62. the methods to appear in autocomplete. You can't prevent the fake metatable
  63. from being edited, unfortunately.
  64. • On that note, if you set your own '__metatable' to anything else, you should
  65. avoid setting it to anything other than a table, a string, or 'false'.
  66. • If you want 'getmetatable' to return something completely immutable other than
  67. the default 'false', use a string because it has metamethods so you won't run
  68. into these problems.
  69. • The class constructor has been reworked to use the 'readOnly' function for
  70. setting its '__metatable'.
  71.  
  72. Part 3: Diagnosing and Fixing the Second Problem
  73. • On another note, '__newindex' no longer stops existing public elements from
  74. being changed, but it still stops new elements from being added.
  75. • It also now stops elements from being added or removed with 'table.insert' and
  76. 'table.remove', so I've updated my metatables guide and the documentation for
  77. this script with the new info. 'protected = true' can still be used.
  78. • This also means that existing public elements are now mutable, even if they're
  79. protected. This also doesn't stop elements from being removed by assigning
  80. them to nil.
  81.  
  82. Part 4: Diagnosing and Fixing the Last Two Problems
  83. • Something has also apparently changed with '__len', so I've updated the
  84. default function to stop it from causing stack overflows.
  85. • Apparently looping through the indecies goes through the parent table and
  86. '__index' as if they were one table. So now the default '__len' only returns
  87. the single length value. That works out since it also apparently can return no
  88. more than a single value now.
  89. • I realized the read-only table was being applied to '__index' twice. Thanks to
  90. the changes I made to '__len' this is no longer needed in the object
  91. constructor so this is now handled solely by 'readOnly'.
  92.  
  93.  
  94. 2023/09/03:
  95. • Constructor method now runs before the readOnly function, so that the
  96. constructor can set up public members before they become protected.
  97. • Protected public members can't be changed after the object is created. If you
  98. want members that are immutable externally but not internally, use readOnly.
  99. • Fixed a typo in the documentation.
  100. • Changed 'metatable' to the correct metamethod '__metatable' in the class
  101. constructor.
  102. • Changed '__metatable' from an empty string to an empty function in the class
  103. constructor and in the readOnly function.
  104.  
  105.  
  106. 2023/07/08:
  107. • Removed the commented code at line 26.
  108. • Removed a little bit of excess white space.
  109. • Cleaned up some typos and verbage in the changelog.
  110.  
  111.  
  112. 2023/03/10:
  113. • Fixed 'typeCheck' not throwing error messages in the correct scope.
  114. • Edited some parts of the documentation to fix typos and be slightly more
  115. concise.
  116.  
  117.  
  118. 2023/03/09:
  119. • Fixed a bug in which the object was not getting returned from the object
  120. constructor.
  121.  
  122.  
  123. 2023/03/08:
  124. • This update completely fixes inheritance, which I broke several udpates ago
  125. with the more efficient utilization of metatables.
  126. • This update completely overhauls the program, and it will break any programs
  127. that used previous versions (again), so make sure to update all your programs
  128. that use this script if you haven't done so after today's date.
  129.  
  130. • The 'Class' function has been renamed to 'Object' to make way for a new
  131. 'Class' function. The object constructor can still be used for its original
  132. purpose, however inheritiance has been removed because I've changed how
  133. inheritance works.
  134. • The new 'Class' function is the class constructor, which can be used to create
  135. classes from "headers" with a syntax much more similar to headers in C++.
  136.  
  137. • Make your class definitions inside a function that returns a table using the
  138. correct format (same as it's always been, minus the 'inherit' table). This
  139. function will serve as a header that is passed to the class constructor and a
  140. class will automatically be created.
  141. • When you call the class as a function, it will pass the header into the
  142. object constructor, which will create an object from the class.
  143. • You can still create your class table manually and pass that directly to the
  144. object constructor.
  145. • If you instead call the method '.h()' from the class it will return a table
  146. with all of the class definitions. This can then be used to create
  147. inheritance. A full guide on how this works can be found here:
  148. https://pastebin.com/upj3fgyZ
  149.  
  150.  
  151. 2023/02/26:
  152. • This update is my solution for the problem mentioned in yesterday's udpate.
  153. It's a big one and and it comes in multiple parts for easier readability.
  154.  
  155. Phase 1:
  156. • I've realized I was using __newindex wrong before I learned how metamethods
  157. are supposed to work. It has been reimplemented with a default state.
  158. • The 'protected' key has been renamed to 'readOnly'. Make sure to update all
  159. your programs that use this script if you haven't done so after today's date.
  160. • The 'protected' key is now a boolean value.
  161. • When 'protected' == true __newindex will be a function that throws an error if
  162. the user attempts to change public members.
  163. • If you set your own __newindex, it will be overwritten if 'protected' == true.
  164.  
  165. Phase 2:
  166. • By default, __len will return the length of both the public and read only
  167. tables, instead of just either or.
  168. • The arguments for readOnly() have been rearranged. 'meta' comes before 'ro'.
  169. • Added an 'invert' argument to typeCheck(). If set to true, it will throw an
  170. error if the type of value IS equal to 'types' rather than is NOT equal.
  171. • Added comments with new documentation of how to use this script.
  172.  
  173. Phase 3:
  174. • There are no longer any required defaults for metamethods.
  175. • __index will be nil by default.
  176. • __index will be overwritten by the readOnly member, but not by its default.
  177. • You can now set __metatable to whatever you want. Setting it to nil would be
  178. the same as not having it at all, which defaults to "" as that is the closest
  179. equivalent to nothing without setting it to nil.
  180.  
  181. Phase 4:
  182. • To recap what hasn't changed since the last update:
  183. • Inheritance still only works for public members. Read only members and
  184. metamethods will need to be added manually.
  185. • By default, __call will be an empty function that does nothing.
  186. • __call will be overwritten by the member 'ctor', but not by its default.
  187. • __len will not be overwritten by its default.
  188. • All metamethods not mentioned here are free to use for whatever you want.
  189.  
  190.  
  191. 2023/02/25:
  192. • The change I made yesterday to the readOnly function broke inheritance. It is
  193. no longer possible for classes to inherit protected members. Inheritance will
  194. be restricted to public members only until I can come up with a fix or
  195. workaround for this.
  196. • Reworked how the __call metamethod is handled.
  197. • The object will set __call to members.ctor if members.ctor has been defined.
  198. • If members.ctor is not defined but __call is defined then it stays as is.
  199. • If __call is not defined then a default function that does nothing will be
  200. set.
  201.  
  202.  
  203. 2023/02/24:
  204. • readOnly() - Swapped __newindex for __metatable for protected members.
  205. • Protected members are no longer accessible through getmetatable.
  206. • __newindex is now available for use in the meta member, and using __metatable
  207. should instead be avoided.
  208. • __len will have a default function that can be overwritten by the object
  209. class.
  210.  
  211.  
  212. 2023/02/23:
  213. • Added argument to readOnly() to allow metamethods.
  214. • Added argument to Class to allow metamethods.
  215. • Changed first error check in Class to be handled in typeCheck().
  216. • Added "ctor" member for constructor methods.
  217. • Avoid using the __index, __newindex, and __call metamethods, as they will be
  218. overwritten by the object constructor.
  219. • Removed example code to reduce file size.
  220. • Reformatted the changelog so it's a bit cleaner than before.
  221.  
  222.  
  223. 2023/02/08:
  224. • Added some comments at the top about the pastebin link and the directory this
  225. file should be saved in a ComputerCraft computer.
  226.  
  227.  
  228. 2021/08/18:
  229. • Exposed 'typeCheck' as a global function.
  230. • Added a comment indicating where example code starts.
  231. • Updated the documentation.
  232.  
  233.  
  234. 2021/07/30:
  235. • Fixed a bug caused by a typo.
  236.  
  237.  
  238. 2021/07/29:
  239. • The code that sets protected variables as a read-only metatable has been moved
  240. from the object constructor to its own global function so it can be used to
  241. set read-only tables beyond the scope of the object constructor.
  242. • I've appended the end of the instructions about members to reflect this
  243. change: I found a valid use case for different members with the same name.
  244. • A read-only protected metatable can be used to reference a private table of
  245. the same name without allowing it to be changed externally.
  246.  
  247.  
  248. 2021/07/28: Day 1 patch:
  249. • Fixed a bug from a last minute change I did before I posted it on pastebin.
  250. • Added an instruction to the comments about protected table variables that I
  251. didn't think of until now.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement