get struct member names

2015-03-14 Thread Charles Cooper via Digitalmars-d-learn
Hi all, I am new to D and so far it is really great. I am wondering how to get the names of member variables in a struct or class. I haven't worked it out yet but this would enable metaprogramming like iterating over the members of the struct. I see in std.traits that you can get get MemberF

'strong types' a la boost

2015-03-14 Thread Charles Cooper via Digitalmars-d-learn
I was wondering what the idiomatic D way of implementing strong types. Boost has something along these lines using classes: http://www.boost.org/doc/libs/1_37_0/boost/strong_typedef.hpp When programming in C++ I find that the compiler does not necessarily generate good code with these types, an

Re: get struct member names

2015-03-14 Thread Charles Cooper via Digitalmars-d-learn
Wow, this is f***ing cool. http://dlang.org/traits.html#allMembers Thank you! On Saturday, 14 March 2015 at 14:54:27 UTC, Adam D. Ruppe wrote: __traits(allMembers, Struct) can do it. Get the free sample chapter from my book: https://www.packtpub.com/application-development/d-cookbook and it

Re: 'strong types' a la boost

2015-03-14 Thread Charles Cooper via Digitalmars-d-learn
I think I may have answered my own question. It seems std.typecon provides a facility for this. http://dlang.org/phobos/std_typecons.html#.Proxy http://dlang.org/phobos/std_typecons.html#.Typedef Is this the 'right' way to do things? It seems that Proxy is used as a mixin whereas Typedef is use

Re: 'strong types' a la boost

2015-03-14 Thread Charles Cooper via Digitalmars-d-learn
Interesting. I think in the second example there are pathological cases where one has similar declarations in two modules at the same line. moduleA.d:100 alias dollars_t TypeDef!int; moduleB.d:100 alias cents_t TypeDef!int; main.d: import moduleA; import moduleB; void write_dollars_to_database

get from tuple by type

2015-03-15 Thread Charles Cooper via Digitalmars-d-learn
C++14 has: template constexpr T& get(tuple& t); Which allows you to get a member of the tuple struct by type. Is there an idiomatic / library way to do this in D? Preferably by indexing. Here is what I have, it is ugly but works: /* CODE */ static import std.stdio; static import std.typecons;

Re: get from tuple by type

2015-03-15 Thread Charles Cooper via Digitalmars-d-learn
foo[1] is sometimes better, but not always. One has to go back to the definition of the thing and literally calculate by hand which element of the tuple you want, and then try compiling it, and so forth. Although the type system will guarantee that you eventually get it right it is a waste of t

Re: get from tuple by type

2015-03-15 Thread Charles Cooper via Digitalmars-d-learn
Sure. It is also easy to write merge sort. Or std.typetuple.Erase. Or Tuple.opIndex(size_t). But that doesn't mean everybody does it. Some utilities (and I am not saying this is, but it could be) are widely used enough that it makes sense to put them in the standard. On Sunday, 15 March 2015

Re: get from tuple by type

2015-03-15 Thread Charles Cooper via Digitalmars-d-learn
Thanks for the style recommendations. On Sunday, 15 March 2015 at 23:14:32 UTC, anonymous wrote: I don't think there is. I don't know if there should be. Distinguishing tuple fields by their type doesn't seem very useful to me, since multiple fields can have the same type. Using combined synt

Re: get from tuple by type

2015-03-15 Thread Charles Cooper via Digitalmars-d-learn
True. If I had to do something involving such an API I would first wrap the API with a type safe one before doing anything else. void external_api_do_something(uint dollars, uint cents); /* I think this could somehow be automated with staticMap and ParameterTypeTuple / ParameterIdentifierTuple

Re: get from tuple by type

2015-03-15 Thread Charles Cooper via Digitalmars-d-learn
Not offended at all :), in fact it was not even my suggestion that it be included in the standard. I was just knee jerk reacting to the comment that, just because something is simple to do precludes it from getting standardized On Sunday, 15 March 2015 at 23:28:18 UTC, ketmar wrote: sorry if

Re: get from tuple by type

2015-03-15 Thread Charles Cooper via Digitalmars-d-learn
http://dlang.org/phobos/std_variant.html#.Algebraic Thanks! This is fascinating, really a breath of fresh air coming from the C++ way of doing things. On Sunday, 15 March 2015 at 23:31:59 UTC, bearophile wrote: If you are experiencing those problems it's probably the way D/Phobos to tell you t