Return to Snippet

Revision: 2019
at December 18, 2006 08:06 by kergoth


Initial Code
function deprecate(o,name,instead)
	local msg = "%sWarning, deprecated feature "..(name and ("'"..name.."' ") or "").."in use."..(instead and ("  Use '"..instead.."' instead.") or "")
	if(type(o) == "table") then
		return setmetatable({},{__index = function (t,k)
				local _,callpoint = pcall(function() error("",4) end)
				print(string.format(msg,string.gsub(callpoint,"(^w%*%.%w*%:%d+)","%1")))
				return o[k]
			end,
			__newindex = function (t,k,v)
               	local _,callpoint = pcall(function() error("",4) end)
				print(string.format(msg,string.gsub(callpoint,"(^w%*%.%w*%:%d+)","%1")))
				o[k] = v
				return o[k]
			end
		})
	elseif(type(o) == "function") then
		return function(...)
			local _,callpoint = pcall(function() error("",4) end)
			print(string.format(msg,string.gsub(callpoint,"(^w%*%.%w*%:%d+)","%1")))
			return o(unpack(arg))
		end
	end
end
Ace = {}

function Ace:print(msg)
	print(msg)
end

function foo()
	print("FOO!")
end
ace = deprecate(Ace,"ace","Ace")
FOO = deprecate(foo,"FOO","foo")

FOO()
ace:print("moo")

./deprecate.lua:38: Warning, deprecated feature 'FOO' in use.  Use 'foo' instead.
FOO!
./deprecate.lua:39: Warning, deprecated feature 'ace' in use.  Use 'Ace' instead.
moo

Initial URL


Initial Description


Initial Title
Lua: API deprecation

Initial Tags


Initial Language
Other