Comments (4)
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.
Unfortunately I don't see anything how we could make things easier without writing a whole book on this topic. IMO it's still useful for C++ and or other advanced D-newbies..
from english.
Yes this is a complex template but it will be difficult to really dive into details in form a tour gem
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.
his 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 agree ;-)
from english.
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. std.meta
+ std.traits
is what I would call pure metaprogramming
from english.
From @ZombineDev on June 26, 2016 7:29
If you want to show is
, I would suggest implementing a JSON <-> struct | struct[] | struct[key] serialization / deserialization. AFAIR, last time I did it, it was less than 60 lines. If you want, I can dig up my code.
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 english.
Related Issues (20)
- Raw string error
- Add D in action chapter HOT 3
- Language selection on top
- Is text description for Casear encryption wrong? HOT 2
- on basics/type-qualifiers - example's writeln output consistency - proposal HOT 2
- Add a page on pattern matching HOT 1
- on basics/ranges - possible wrong instance HOT 1
- on gems/functional-programming use of deprecated resources - replacement proposal HOT 2
- byexample/code-generation-parser example doesn't compile HOT 5
- Doc examples doesn't run due to dependency error HOT 3
- `ld` throw error when `dub run dlang-tour -- --lang-dir .` HOT 3
- Proposal for a better opApply example
- Multidimensional Array declaration
- Page got grieffed on dlang.org HOT 3
- vibed/web-server.md code example build error. HOT 1
- Need much, much better instructions HOT 3
- Set up test integration HOT 1
- Merge Loops into Control Flow
- Improve Imports and Modules
- Reorganize Learning Path for new learners HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from english.