lua-users home
lua-l archive

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


On 31 Jan 2011, at 14:02, David Given <dg@cowlark.com> wrote:
> 
> Something like this to enforce the interfaces might be handy:
> 
> #if DEBUG
> local function CastToGeometrical(o)
>    local oo = {
>        __index = oo,
>        GetGeometry = function(self)
>            return o:GetGeometry()
>        end,
>        SetGeometry = function(self, g)
>            return o:SetGeometry(g)
>        end
>    }
>    setmetatable(oo, oo)
>    return oo
> end
> #else
> local function CastToGeometrical(o)
>    return o
> end
> #endif
> 
> function Widget:ResetGeometry(g)
>    local g = CastToGeometrical(g)
>    g:SetGeometry(Empty) -- works
>    g:Redraw() -- produces run time error
> end

I wonder what you could call an anti-duck-typing pattern ... :-)

I think a more idiomatic way of coding this would be to leave the mainline code uncluttered with sanity checks, and instead rely on an extensive test suite.

Tony.
--
f.anthony.n.finch  <dot@dotat.at>  http://dotat.at/