Hacker News new | ask | show | jobs
by binary132 10 days ago
It’s also pretty nice that a verb plus a table is implicitly a function call. This means you can easily do things that seem to be types:

  m = Menagerie {
    Dog { tail, ears, snout=long },
    Cat { you, get, the=idea },
    table.unpack(ark),
  }
2 comments

This syntax is inherited from one of Lua's predecessors, Sol, for compatibility, and in Sol, it actually means types!

There are a couple of snippets here that refer to it (search for @track):

https://www.lua.org/history.html

You can go further and even register your actual C++ types (e.g. with sol2), which removes lots of extra parsing steps:

    pos = Point(2.5, 8.9)
    size = Size(4, 5)
    opacity = 0.5
    color = Color(0.5, 0.2, 0.1)
C types as well (with LuaJIT.). Building metaclasses around SDL objects and adding a tostring function to they can all be printed, for instance. It's so much easier to build a C API in Lua than it is to build a Lua API in C.
You don’t need anything special to do that, you can just export Lua functions that return userdata