lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




Am 20.09.2017 um 13:28 schrieb Luiz Henrique de Figueiredo:

local types = {
"nil", "boolean", "number", "string", "table", "function", "userdata", "thread",
}

is = {}

for _,t in pairs(types) do
	is[t] = function (v)
			if type(v)~=t then
				error(t.." expected, got "..type(v),2)
			end
			return v
		end
end

function f() end
x=2
s="a"
s=2
f (is.number (x), is.string (s))


I like it! But i still i dont like any prefix solution / funtion call for this task. I would like to write in my sources something like:
  f(x.string(), y.number())
or better:
  f(x.string, y.number)
hence we have to modify the metatables for all data types. Each metatable needs only 1 method: string() for string metatable, number() for number metatable and so on. This function should return the value itself. If i call for instance x.string() on a number value, the method is undefined and a error is raised.

May be i dont see the drawbacks of this idea?

Ulrich.