-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
too complicated gems/template-meta-programming #34
Comments
From @stonemaster on June 25, 2016 15:17 Yes this is a complex template but it will be difficult to really dive into details in form a tour gem. This gem is in my eyes more directed to people who already this technique from languages like e.g. C++. And for those interested there are far better (and longer articles) that explain this complicated topic in more detail. |
I think the main problem here is that the example is too complicated (it has 65 lines). We should try to get it to half of the size. At the moment I don't have concrete ideas though.
I agree ;-) |
From @ZombineDev on June 26, 2016 7:11 Here's a simpler and more powerful version of the example: import std.algorithm.searching : countUntil;
import std.meta : Repeat;
struct VecDef(T, names...)
{
enum N = names.length;
T[N] _elements;
this (Repeat!(N, T) elems)
{
_elements = [elems];
}
ref inout(T) opDispatch(string field)() inout
{
enum size_t idx = [ names ].countUntil(field);
return _elements[idx];
}
}
unittest
{
alias Size = VecDef!(uint, `width`, `height`);
auto s = Size(2, 3);
s.width++;
s.height++;
assert (s.width + s.height == 7);
alias Color = VecDef!(ubyte, `r`, `g`, `b`, `a`);
Color c = Color(15, 17, 19, 3);
assert (c.r == 15);
assert (c.g == 17);
assert (c.b == 19);
assert (c.a == 3);
alias vec3f = VecDef!(float, `x`, `y`, `z`);
auto speed = vec3f(0.25, 0.5, 0.125);
assert (speed.x == 0.25);
assert (speed.y == 0.5);
assert (speed.z == 0.125);
} But, btw this is not template metaprogramming, but typecons. |
From @ZombineDev on June 26, 2016 7:29 If you want to show If you want to show more general introspection, use DbI (e.g. implenting a range that offers all possible primitives depending on the input) or UDAs (e.g. defining a REST interface, or something along the lines of Robert's talk at DConf http://dconf.org/2016/talks/schadek.html) |
From @wilzbach on June 24, 2016 16:48
for a ruby/java guy this looks complicated :
(reported by @claudiug)
Copied from original issue: dlang-tour/core#366
The text was updated successfully, but these errors were encountered: