www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Are Lua tables possible to do with D?

reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
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
Jul 15 2015
next sibling parent reply "Fusxfaranto" <fusxfaranto gmail.com> writes:
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
Jul 16 2015
next sibling parent =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
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
Jul 16 2015
prev sibling parent "rcorre" <ryan rcorre.net> writes:
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"); } ---
Jul 17 2015
prev sibling parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
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
Jul 16 2015