[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Help when using Interfaces (fork from table lib)
- From: Dirk Laurie <dirk.laurie@...>
- Date: Thu, 28 Nov 2013 08:27:13 +0200
2013/11/28 Andrew Starks <andrew.starks@trms.com>:
> What really happens, and where I get stuck , is in cases where I want to
> provide flexibility in the interface to a function.
...
> Example:
>
> I have an object that holds a sequence. I would like to provide a way to
> accept that object or a sequence of them. Knowing that I have one object or
> many can be non-trivial, especially if I'm likely to get them from somewhere
> other than a typical lua oo implementation.
...
> Another example is a string. Everything can have tostring called on it. If I
> want my function to accept a table or a string, how do I know that I don't
> have an object that is trying to quack like a string?
Is XML lurking under the surface here?
> In Lua. 5.3 or in the future, it'd be cool if making flexible interfaces to
> functions could be made simpler, without sacrificing polymorphism.
I'm not sure I understand exactly what a flexible interface would be like,
but the examples suggest something like this:
function handler(thing)
if type(thing)~='table' then -- handle scalar case
elseif thing.handler_can_handle_it then -- handle object case
else
result={} -- if required
for k=1,#thing do
handler(thing[k])
-- or maybe -- thing[k]=handler(thing[k])
-- or maybe -- result[k]=handler(thing[k])
end
return result
-- or maybe -- return thing
end
end
Is that what you are after?