lua-users home
lua-l archive

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


Karel Tuma wrote:
[...]
> tab[do_something.."_"..type(arg1).."_"..type(arg2)](arg1,arg2)
[...]
> similiarly as for argument type constraints, this is partially implementable
using
> metatables, but not to the degree i'd call satisfactory.

Untested, but:

t = {}
setmetatable(t, t)
function t:__index(key)
	return function dispatch(...)
		local k = {key}
		for _, j in ipairs(arg) do
			k[#k+1] = type(j)
		end
		k = table.concat(k, "_")
		return rawget(self, k)(unpack(arg))
	end
end

Now, whenever you call a method k on t, it'll actually attempt to call
k_type1_type2_type3... instead. This strikes me as being rather simple code
--- why do you consider such an approach unsatisfactory?

I don't believe that such a thing belongs into the parser. Nobo

[...]
> function fun(s($==1 or $=="a"), n isnumber($))
> 	local s2 (isstring($) and #$>5) = somethingcool(s)
> 	return somethingboring(s,s2,n) isstring($)
> end

Again, what's the difference between this and:

function fun(s, n)
	assert((s==1 or s=="a") and isstring(n))

	local s2 = somethingcool(s)
	assert(isstring(s2) and (#s2 > 5))

	local r = somethingboring(s, s2, n)
	assert(isstring(r))
	return r
end

...? I find this considerably clearer to read, as well as not needing any new
features. It makes it explicit the order of processing, for example --- in
your example, the 'local' line has the assertions on the left of the
expression, where the return line puts them on the right, which isn't terribly
clear.

-- 
+- David Given --McQ-+ "There is one thing a man must do // Before his
|  dg@cowlark.com    | life is done; // Write two lines in APL // And make
| (dg@tao-group.com) | the buggers run."
+- www.cowlark.com --+ --- The Devil's DP Dictionary

Attachment: signature.asc
Description: OpenPGP digital signature