Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Can you elaborate on the bool vs enum point? I don't get it.


C++ function to compare two enums: bool operator ==(SomeEnum left, SomeEnum right) { return left == right; }

In my bindings (in C++): LM_BIND_NONMEMBER_OP(SomeEnum, (==))

In Io script: if( obj property == SomeEnumInstance, doStuff())

The == in the script calls the overloaded == operator defined in C++, via the binding. Turns out, you do a lot of this in game programming, at least with Irrlicht (or maybe it's a quirk of what I'm doing in my game scripts). Testing an enum for equality is cheap in C++. Calling == from script via a binding adds an order magnitude (or two!) more overhead. I could improve this by optimizing for this case.

(You may wonder - why I don't just convert enums to script numbers, which surely are faster. Mainly because I wrote my Io to C++ binding library to be strongly typed; it does not allow mixing a number with an enum and if you auto-demoted enums to numbers in script that type information would be lost. And I don't think Io's interpreter goes to the same lengths as, say, a Smalltalk does to optimize for numbers; Io takes an even more purist approach to treating everything as an object.)


LuaJIT's ffi lets you use native C types directly in Lua, which fixes that issue and keeps the type info.


He's making a C API call to essentially compare two integers instead of just comparing them directly in Lua. You can fix that by pushing the enum values to Lua as numbers instead of userdata with metatables for comparisons.


Well it's Io but you're still correct, I could fix that by letting enums "decay" into Numbers. In fact, I originally had that happen accidentally and I actually considered it a bug. The reason I considered that a bug was that I made a design decision to strongly type everything that goes across the boundary between script and C++.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: