lua-users home
lua-l archive

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


On Fri, Aug 01, 2014 at 03:04:33PM -0500, Steven Degutis wrote:
> On Fri, Aug 1, 2014 at 2:57 PM, Sean Conner <sean@conman.org> wrote:
> >   One situation I hit in C was the following:
> >
> >         static int foobar___index(lua_State *L)
> >         {
> >           foobar__s *foo = luaL_checkudata(L,1,TYPE_FOO);
> >
> >           if (lua_isstring(L,2))
> >             ...
> >           else if (lua_isnumber(L,2))
> >             ...
> >         }
> >
> > Odd bugs until I actually read the description for lua_isstring():
<snip>
> Oh wow. That's actually pretty unsettling. I remember reading in PIL
> (third edition) to be careful about this, and even seeing it a second
> time is still a bit unsettling.
> 

A number is a much stricter type than a string. For me it's natural to check
for an integer first before a string. But I also religiously keep language
documentation open on my desktop (for Lua, C, POSIX, etc).

I'm not a C++ programmer, but there's a parallel with the ranking of
implicit conversions when dealing with overloaded functions.

Because of Lua's metamethod devices it's important to always be in the habit
of generating a loose hierarchy of types in your mind when implementing
function overloading. Otherwise they go to waste. If implicit coercions
didn't have significant value, neither would metamethods.

Yes, often times you're trying to cram a square peg into a round hole. But
it's darned convenient. It's up to the programmer to rank things so he tries
fitting the round peg into the round hole first. Abstractions have costs
because they necessarily obscure context. But that's their value!

I feel like the obsession with stricter typing is turning into an
anti-pattern.