Thread overview
Are Lua tables possible to do with D?
Jul 16, 2015
Robert M. Münch
Jul 16, 2015
Fusxfaranto
Jul 16, 2015
Robert M. Münch
Jul 18, 2015
rcorre
Jul 16, 2015
Jesse Phillips
July 16, 2015
Hi, do you think it's possible to implemented something like Lua Tables (a hashed heterogeneous associative array) in D?

I know that Lua is dynamic and interpreted, hence it's a lot simpler to do than with a compiled language but I'm wondering if we could express such a generic data-structure in D.

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster

July 16, 2015
On Thursday, 16 July 2015 at 06:48:12 UTC, Robert M. Münch wrote:
> Hi, do you think it's possible to implemented something like Lua Tables (a hashed heterogeneous associative array) in D?
>
> I know that Lua is dynamic and interpreted, hence it's a lot simpler to do than with a compiled language but I'm wondering if we could express such a generic data-structure in D.

An associative array of Variant[string] ought to do the job well enough.

http://dlang.org/phobos/std_variant.html
July 16, 2015
On 2015-07-16 07:20:15 +0000, Fusxfaranto said:

> An associative array of Variant[string] ought to do the job well enough.
> 
> http://dlang.org/phobos/std_variant.html

Thanks a lot. Somehow didn't see that...

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster

July 16, 2015
On Thursday, 16 July 2015 at 06:48:12 UTC, Robert M. Münch wrote:
> Hi, do you think it's possible to implemented something like Lua Tables (a hashed heterogeneous associative array) in D?
>
> I know that Lua is dynamic and interpreted, hence it's a lot simpler to do than with a compiled language but I'm wondering if we could express such a generic data-structure in D.

Check out LuaD[1], it is more about conversions, but you can convert to a algebraic structure, I need to revisit this now that Algebraic types can be self referencing.

1. https://github.com/JakobOvrum/LuaD
July 18, 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");
}
---