Are Lua tables possible to do with D?

rcorre via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 17 19:40:10 PDT 2015


On Thursday, 16 July 2015 at 07:20:16 UTC, Fusxfaranto wrote:
>
> An associative array of Variant[string] ought to do the job 
> well enough.
>
> http://dlang.org/phobos/std_variant.html

For extra fun, you can implement the '.' style syntax pretty 
easily:

---
import std.variant;

struct LuaTable {
   Variant[string] _table;
   alias _table this;

   auto opDispatch(string s)() {
     return _table[s];
   }

   void opDispatch(string s, T)(T val) {
     _table[s] = Variant(val);
   }
}

unittest {
   LuaTable table;

   table.foo = 5;
   table.bar = "s";

   assert(table.foo == 5);
   assert(table.bar == "s");
}
---


More information about the Digitalmars-d-learn mailing list