lua-users home
lua-l archive

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

On Sat, Mar 15, 2014 at 7:54 AM, David Crayford <> wrote:
> I had the same epiphany, but given the choice of straight C or C++ I would
> opt for the latter every time.

It's very true that a higher-level C++ API will be safer. E.g. this is
the kind of scheme any half-competent C++ programmer could organize in
an hour or so:

LState state(L);
LRef strfind(L,"string.find");
int i1,i2;
state << "hello dolly" << "dolly";,2);
state >> i1 >> i2;

where the familiar iostream pattern is now overloaded to mean pushing
and popping stack values.

Can push the idea further with some auxiliary classes, if you like one-liners; << "hello dolly" << "dolly") >> i1 >> i2;

Operator/function overloading means that what gets pushed is defined
by the argument type, rather than by type specifiers, which may or may
not match what was actually passed, since to get flexibility we have
to switch off C's already weak type checking:

llua_callf(strfind,"ss","hello dolly","dolly","ii",&i1,&i2);

But that's C for you; we're always a little typo away from disaster ;)

steve d. (who does know C++, despite his protestations...)