[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Are automatic coercions ever going away?
- From: Sean Conner <sean@...>
- Date: Fri, 1 Aug 2014 18:19:26 -0400
It was thus said that the Great William Ahern once stated:
> 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).
And I'm so used to non-coersion [6] that it never crossed my mind and
well, it lead to bugs.
> 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.
I don't think that way. I don't use metatables for tables, I use them for
userdata, and for all the userdata interfaces I've written, I've found one
that overrides something other than __call, __index, __newindex, __gc,
__tostring or __len [7], that's how rarely I do overloading. [8]
I almost overrode __concat (or __add, I was dithering) for a protocol
handler I was writing, to add TLVs [9] to an outgoing packet, but I decided
against it at the last moment as "being too cute."
> 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.
I've checked the source control logs and over the past month, I've logged
50 bugs in a work related project written in Lua [1]. Four of the bugs are
work arounds to bugs in other components I have no control over; two were
reverted (found not to be a bug or the wrong fix for a bug); 31 were logic
or misunderstandings (in other words, a compiler would not have caught
these) and the remaining 13 were bugs that a compiler or stricter typing
would have caught. [3]
-spc (Who likes his footnotes)
[1] It processes SIP messages. Man, is SIP an annoying protocol. [2]
[2] Read: I don't know the SIP protocol like the back of my hand and
I really don't want to read 121 RFCs.
[3] Now, one might reasonably ask why 13 typos got into the source
control reposity. I might answer as that's the only way to get the
code to the target machine for interoperability testing [4]. This
is also the first time I've seen actual SIP messages from the wild
[5].
[4] Seriously. I need to run at least three other programs to even
start testing this code.
[5] Equipment from other companies we need to interoperate with.
[6] Assembly was my second language I learned; I've been using C for 25
years now. I've only been using Lua for a few years.
[7] POSIX signal sets. I defined __add, __sub and __umn methods.
https://github.com/spc476/lua-conmanorg/blob/9be9c1145184aad515864c5a50ba6a7f64b4e418/src/signal-posix.c#L1147
[8] For operators like '+' or '*'.
[9] Tag, Length, Value. It's a popular format where I work.