dibyendumajumdar / ravi

Ravi is a dialect of Lua, featuring limited optional static typing, JIT and AOT compilers
http://ravilang.github.io/
Other
1.16k stars 60 forks source link

Add `debug.registrytype(type, mt)` function #226

Open snoopcatt opened 3 years ago

snoopcatt commented 3 years ago

Hello! I suggest to add a bit sugar and create separate function to register new type.

How it looks now:

  -- Create a metatable
  local mt = { __name='MyType'}

  -- Register the metatable in Lua registry
  debug.getregistry().MyType = mt

My suggestion:

  -- Register new type in Lua registry and get associated metatable
  local mytype = debug.registertype('MyType', { __name='MyType'}) -- returns metatable
  -- local mt = {__name='MyType'}; debug.registertype('MyType', mt); -- also acceptable
snoopcatt commented 3 years ago

Maybe it is better to name this function debug.settype, by analogy with

dibyendumajumdar commented 3 years ago

Hi, I want to add some facility to create classes easily - but it cannot be something that relies on debug api.

Best approach might be to see an existing library that does the elegantly.