Re: How do I iterate over enum members at runtime?

2011-04-09 Thread Andrej Mitrovic
On 4/10/11, Jesse Phillips wrote: > Andrej Mitrovic Wrote: > >> It's a shame that an enum with a tag doesn't have a .length property. >> I've had to use __traits to build an array just to get the length. > > It has .max > > http://www.digitalmars.com/d/2.0/enum.html > That doesn't show me the num

Re: How do I iterate over enum members at runtime?

2011-04-09 Thread Jesse Phillips
You must iterate a compile time only construct. So yes it would be nice to be able to say: static foreach and have the compiler tell you, no I can't do that (and force it if it can). Andrej Mitrovic Wrote: > Well in this case it works, but it's not always so easy. For example: > > import std.

Re: How do I iterate over enum members at runtime?

2011-04-09 Thread Jesse Phillips
Andrej Mitrovic Wrote: > It's a shame that an enum with a tag doesn't have a .length property. > I've had to use __traits to build an array just to get the length. It has .max http://www.digitalmars.com/d/2.0/enum.html

Re: How do I iterate over enum members at runtime?

2011-04-09 Thread Andrej Mitrovic
Well, the C code I was translating had something like this: struct SysMetrics { int iIndex; char* szLabel; char* szDesc; } And then it used an array of these structures. So for all existing enums that started with "SM_", those fields were populated with the enum value and a name and de

Re: How do I iterate over enum members at runtime?

2011-04-09 Thread Cliff Hudson
You could probably make a template out of the ugly __traits invocation as well? On Sat, Apr 9, 2011 at 3:37 PM, Andrej Mitrovic wrote: > On 4/10/11, Jonathan M Davis wrote: > >> What the.. I was sure mixin wouldn't work in a foreach loop. > > > > Whyever not? mixins work in most places. I believ

Re: How do I iterate over enum members at runtime?

2011-04-09 Thread Andrej Mitrovic
On 4/10/11, Jonathan M Davis wrote: >> What the.. I was sure mixin wouldn't work in a foreach loop. > > Whyever not? mixins work in most places. I believe that the problem is that > they have to be a whole expression or statement rather than just a piece of > one, so sometimes you can't use a mixi

Re: How do I iterate over enum members at runtime?

2011-04-09 Thread Jonathan M Davis
> What the.. I was sure mixin wouldn't work in a foreach loop. Whyever not? mixins work in most places. I believe that the problem is that they have to be a whole expression or statement rather than just a piece of one, so sometimes you can't use a mixin for something small and have to put more

Re: How do I iterate over enum members at runtime?

2011-04-09 Thread Andrej Mitrovic
What the.. I was sure mixin wouldn't work in a foreach loop. Thanks, Jesse!

Re: How do I iterate over enum members at runtime?

2011-04-09 Thread Jesse Phillips
On Sat, 09 Apr 2011 16:20:06 -0400, Andrej Mitrovic wrote: > I know there's traits to get strings at compile time, e.g.: auto b = [ > __traits(allMembers, Metrics) ]; > > but this doesn't help me try out those enum values at runtime. It could > almost work if I could use a mixin() in a foreach lo

How do I iterate over enum members at runtime?

2011-04-09 Thread Andrej Mitrovic
E.g.: enum Metrics : int { SM_CXSCREEN = 0, SM_CYSCREEN, SM_CXVSCROLL, SM_CYHSCROLL, SM_CYCAPTION, SM_CXBORDER, } void foo(int m) { } void main() { foreach (m; Metrics) { foo(m); } } This won't work. I know there's traits to

Re: speed of low-level C funcs: example of memmove

2011-04-09 Thread Cliff Hudson
If you take a look at the implementation of memmove (grab the std C lib source) you'll see a rather optimized assembly loop which is very smart about doing machine-word aligned moves, and using processor block-copy instructions. I suspect that is the reason you see the difference. For smaller dat

Re: Anyone have a function to print out the field name and its value?

2011-04-09 Thread Andrej Mitrovic
On 4/9/11, Robert Clipsham wrote: > For now you can compile with -d, this should be reported as a bug though. Ok, reported. http://d.puremagic.com/issues/show_bug.cgi?id=5825 > Dunno, never made sense to me... Could be a question for d.D. Yup. :)

Re: Anyone have a function to print out the field name and its value?

2011-04-09 Thread Robert Clipsham
On 09/04/2011 18:44, Andrej Mitrovic wrote: On 4/9/11, Andrej Mitrovic wrote: That's great, I can use it to print out all the fields. Thanks! Some error checking should be done, or maybe there's a bug. If a field has a type that is a typedef to say a void*: typedef void* HANDLE struct S {

Re: Anyone have a function to print out the field name and its value?

2011-04-09 Thread Andrej Mitrovic
On 4/9/11, Andrej Mitrovic wrote: > That's great, I can use it to print out all the fields. Thanks! Some error checking should be done, or maybe there's a bug. If a field has a type that is a typedef to say a void*: typedef void* HANDLE struct S { HANDLE hnd; } Printing hnd will fail with a

Re: Anyone have a function to print out the field name and its value?

2011-04-09 Thread Andrej Mitrovic
On 4/9/11, Robert Clipsham wrote: > On 09/04/2011 18:23, Robert Clipsham wrote: >> Off the top of my head (untested): >> >> void print(T)(T t) if (is(T == struct) || is(T == class)) >> { >> foreach (i, field; t.tupleof) >> { >> writefln(T.tupleof[i].stringof ~ " = %s", field); >> } >> } >> --

Re: Anyone have a function to print out the field name and its value?

2011-04-09 Thread Andrej Mitrovic
Wow, I figured out a trick. Check it out, two modules: 1. fieldwrite.d: module fieldwrite; import std.string; import std.stdio; import std.conv; mixin template field(string T) { struct FieldTemp { this(string str) { writefln(str ~ " = %s", mixin(T)); }

Re: Anyone have a function to print out the field name and its value?

2011-04-09 Thread Robert Clipsham
On 09/04/2011 18:23, Robert Clipsham wrote: Off the top of my head (untested): void print(T)(T t) if (is(T == struct) || is(T == class)) { foreach (i, field; t.tupleof) { writefln(T.tupleof[i].stringof ~ " = %s", field); } } -- Robert http://octarineparrot.com/ I forgot to mention...

Re: Anyone have a function to print out the field name and its value?

2011-04-09 Thread Robert Clipsham
On 09/04/2011 18:13, Andrej Mitrovic wrote: Let's say I have this structure: struct PAINTSTRUCT { bool state; } And somewhere in my main code I want to print out the value of state. But I also want to know what I'm printing out. So usually I'd do this: void main() { PAINTSTRUCT ps;

Re: speed of low-level C funcs: example of memmove

2011-04-09 Thread spir
On 04/09/2011 07:08 PM, spir wrote: Hello, To insert of delete an array slice, I tried to use C's memmove, thinking it would be far faster than "manually" copying bit per bit (by any kind of magic). But I still wrote a D versions just to check what the actual speed gain is. To my great surprise,

Re: Anyone have a function to print out the field name and its value?

2011-04-09 Thread Andrej Mitrovic
Andrej Mitrovic Wrote: > Actually I couldn't really just use "ps.fErase", because that just passes a bool to the function. Hmm.. this doesn't look possible to do without introducing complexity at the calling site.

Anyone have a function to print out the field name and its value?

2011-04-09 Thread Andrej Mitrovic
Let's say I have this structure: struct PAINTSTRUCT { bool state; } And somewhere in my main code I want to print out the value of state. But I also want to know what I'm printing out. So usually I'd do this: void main() { PAINTSTRUCT ps; writefln("ps.state = %s", ps.state); } Has a

speed of low-level C funcs: example of memmove

2011-04-09 Thread spir
Hello, To insert of delete an array slice, I tried to use C's memmove, thinking it would be far faster than "manually" copying bit per bit (by any kind of magic). But I still wrote a D versions just to check what the actual speed gain is. To my great surprise, the C-memmove and D-manual versio

More alias usages

2011-04-09 Thread bearophile
Inside "static if" blocks do you feel the need of more kinds of alias, like this? void main() { int[] array; int index; alias array.length A; alias 0B; alias index-1 C; // hard to do } Bye, bearophile