Lua: API deprecation


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function deprecate(o,name,instead)
  2. local msg = "%sWarning, deprecated feature "..(name and ("'"..name.."' ") or "").."in use."..(instead and (" Use '"..instead.."' instead.") or "")
  3. if(type(o) == "table") then
  4. return setmetatable({},{__index = function (t,k)
  5. local _,callpoint = pcall(function() error("",4) end)
  6. print(string.format(msg,string.gsub(callpoint,"(^w%*%.%w*%:%d+)","%1")))
  7. return o[k]
  8. end,
  9. __newindex = function (t,k,v)
  10. local _,callpoint = pcall(function() error("",4) end)
  11. print(string.format(msg,string.gsub(callpoint,"(^w%*%.%w*%:%d+)","%1")))
  12. o[k] = v
  13. return o[k]
  14. end
  15. })
  16. elseif(type(o) == "function") then
  17. return function(...)
  18. local _,callpoint = pcall(function() error("",4) end)
  19. print(string.format(msg,string.gsub(callpoint,"(^w%*%.%w*%:%d+)","%1")))
  20. return o(unpack(arg))
  21. end
  22. end
  23. end
  24. Ace = {}
  25.  
  26. function Ace:print(msg)
  27. print(msg)
  28. end
  29.  
  30. function foo()
  31. print("FOO!")
  32. end
  33. ace = deprecate(Ace,"ace","Ace")
  34. FOO = deprecate(foo,"FOO","foo")
  35.  
  36. FOO()
  37. ace:print("moo")
  38.  
  39. ./deprecate.lua:38: Warning, deprecated feature 'FOO' in use. Use 'foo' instead.
  40. FOO!
  41. ./deprecate.lua:39: Warning, deprecated feature 'ace' in use. Use 'Ace' instead.
  42. moo

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.