On Tuesday, 12 August 2014 at 17:36:41 UTC, Maxime
Chevalier-Boisvert wrote:
In my JavaScript VM, I have a function whose purpose is to
expose D/host constants to the JavaScript runtime code running
inside the VM. This makes for somewhat redundant code, as
follows:
vm.defRTConst("OBJ_MIN_CAP"w, OBJ_MIN_CAP);
vm.defRTConst("PROTO_SLOT_IDX"w, PROTO_SLOT_IDX);
vm.defRTConst("FPTR_SLOT_IDX"w, FPTR_SLOT_IDX);
vm.defRTConst("ATTR_CONFIGURABLE"w , ATTR_CONFIGURABLE);
vm.defRTConst("ATTR_WRITABLE"w , ATTR_WRITABLE);
vm.defRTConst("ATTR_ENUMERABLE"w , ATTR_ENUMERABLE);
vm.defRTConst("ATTR_DELETED"w , ATTR_DELETED);
vm.defRTConst("ATTR_GETSET"w , ATTR_GETSET);
vm.defRTConst("ATTR_DEFAULT"w , ATTR_DEFAULT);
I'm just wondering if there's a way to template defRTConst so
that the name of an identifier I'm passing (e.g.: ATTR_DEFAULT)
can be captured by the template, making it so that I don't also
need to pass the name as a string. I expect the answer to be
no, but maybe someone with more knowledge of D template magic
knows better.
Something like this?
enum ATTRS
{
ATTR_GETSET,
ATTR_ENUMERABLE,
ATTR_CONFIGURABLE
}
void foo(ATTRS attr)
{
import std.conv;
foo_impl(attr, to!string(attr));
}
void foo_impl(ATTRS attr, string name)
{
import std.stdio;
writefln("name = %s, attr = %d", name, attr);
}
void main()
{
foo(ATTRS.ATTR_GETSET);
}