Re: Using opDispatch as *magic* getter/setter. Possible or not?

2011-03-31 Thread Jacob Carlborg
On 3/31/11 2:32 AM, Aleksandar Ružičić wrote: Is it possible to use opDispatch as generic getter and setter at the same time? Something like __get() and __set() in PHP.. this is what I've tried: https://gist.github.com/895571 and I get Error: template instance opDispatch!(bar) matches more

Re: Using opDispatch as *magic* getter/setter. Possible or not?

2011-03-31 Thread Aleksandar Ružičić
On Thu, Mar 31, 2011 at 8:52 AM, Jacob Carlborg d...@me.com wrote: Or, I think this will work as well: @property ref ConfigSection opDispatch(string sectionName, Args ...)(Args args) if (Args.length == 0) { // getter } @property ref ConfigSection opDispatch(string sectionName, Args

Re: Using opDispatch as *magic* getter/setter. Possible or not?

2011-03-31 Thread Steven Schveighoffer
On Thu, 31 Mar 2011 02:52:15 -0400, Jacob Carlborg d...@me.com wrote: On 3/31/11 2:32 AM, Aleksandar Ružičić wrote: Is it possible to use opDispatch as generic getter and setter at the same time? Something like __get() and __set() in PHP.. this is what I've tried:

Re: Using opDispatch as *magic* getter/setter. Possible or not?

2011-03-31 Thread Steven Schveighoffer
On Wed, 30 Mar 2011 21:50:43 -0400, spir denis.s...@gmail.com wrote: On 03/31/2011 02:40 AM, Aleksandar Ružičić wrote: 2011/3/31 Aleksandar Ružičićruzicic.aleksan...@gmail.com: Or maybe there is some other way to achive what I want and I'm not aware of it? :-) I know I could have used

Re: template template parameter

2011-03-31 Thread Caligo
I should have been more clear, but my actual question is how do I access the parameters of a template parameter. My example works, but I wanted to know if there is a different and perhaps a better of doing it. In your example it would look something like this: struct SomeContainer(T, int x, int

Re: Using opDispatch as *magic* getter/setter. Possible or not?

2011-03-31 Thread Aleksandar Ružičić
On Thu, Mar 31, 2011 at 1:30 PM, Steven Schveighoffer schvei...@yahoo.com wrote: or you can change the template parameters in the opDispatch setter: @property ref ConfigSection opDispatch(string sectionName, T : string)(T arg) -Steve Thanks, that's much more readable I now have these

A use case for fromStringz

2011-03-31 Thread Andrej Mitrovic
There are situations where you have to call a C dispatch function, and pass it a void* and a selector. The selector lets you choose what the C function does, for example an enum constant selector `kGetProductName` could ask the C function to fill a null-terminated string at the location of the

Re: Using opDispatch as *magic* getter/setter. Possible or not?

2011-03-31 Thread Steven Schveighoffer
On Thu, 31 Mar 2011 14:02:43 -0400, Kagamin s...@here.lot wrote: Steven Schveighoffer Wrote: The issue is that you can't have two templates with the same exact template parameters, even if they have different function parameters. This is because the compiler first instantiates the template,

Re: A use case for fromStringz

2011-03-31 Thread Jesse Phillips
Why not: string getNameOld() { static char[256] name; cDispatch(name.ptr, kGetProductName); return to!string(name.ptr); }

Re: A use case for fromStringz

2011-03-31 Thread Andrej Mitrovic
On 3/31/11, Jesse Phillips jessekphillip...@gmail.com wrote: Why not: string getNameOld() { static char[256] name; cDispatch(name.ptr, kGetProductName); return to!string(name.ptr); } Nice catch! But see my second reply. If a null terminator is missing and we know we're

Re: A use case for fromStringz

2011-03-31 Thread Jesse Phillips
Andrej Mitrovic Wrote: Actually, this still suffers from the problem when the returned char* doesn't have a null terminator. It really sucks when C code does that, and I've just experienced that. There is a solution though: Since we can detect the length of the D array passed into

Re: A use case for fromStringz

2011-03-31 Thread Andrej Mitrovic
Oh I'm not trying to get this into Phobos, I just needed the function so I wrote it and sharing it here. Maybe it should throw. For my purposes I don't need it to throw. :)