Re: Template class with dispatched properties

2013-11-25 Thread Ali Çehreli
On 11/07/2013 10:05 PM, Philippe Sigaud wrote: On Fri, Nov 8, 2013 at 5:55 AM, Ross Hays wrote: And let me say that I really do like that this works in D. I can't imagine doing anything like this in C++ (which is what I used primarily in the past). The only reason I joke about it being useles

Re: Template class with dispatched properties

2013-11-08 Thread bearophile
Chris Cain: One tiny place of improvement for your code, however, is if you changed it to `static immutable offset = ...;` because that helps the compiler know to do that operation at compile time. The idiomatic way to do that is to use "enum offset = ...;". Bye, bearophile

Re: Template class with dispatched properties

2013-11-07 Thread Chris Cain
On Friday, 8 November 2013 at 05:10:57 UTC, Ross Hays wrote: Okay here is something I was hoping for some general clarification on related to this and maybe you can help me sort some things out. The opDispatch method has a template parameter of string fieldName. In C++, templates are actually

Re: Template class with dispatched properties

2013-11-07 Thread Philippe Sigaud
On Fri, Nov 8, 2013 at 5:55 AM, Ross Hays wrote: > And let me say that I really do like that this works in D. I can't imagine > doing anything like this in C++ (which is what I used primarily in the > past). > > The only reason I joke about it being useless is it really only supports > vectors of

Re: Template class with dispatched properties

2013-11-07 Thread Ross Hays
Okay here is something I was hoping for some general clarification on related to this and maybe you can help me sort some things out. The opDispatch method has a template parameter of string fieldName. In C++, templates are actually compiled so each different use of a template class is compil

Re: Template class with dispatched properties

2013-11-07 Thread Ross Hays
And let me say that I really do like that this works in D. I can't imagine doing anything like this in C++ (which is what I used primarily in the past). The only reason I joke about it being useless is it really only supports vectors of length 2 or 3 (technically 1 as well but that is not rea

Re: Template class with dispatched properties

2013-11-07 Thread Ross Hays
Boom, that last few were the issues. I elected to just move the return for the setter onto a separate line, mostly because the idea of auto returning different types seem foreign to me... I have used auto plenty in C++11, but never like that and it just throws me off. But fixing those other mis

Re: Template class with dispatched properties

2013-11-07 Thread Chris Cain
On Friday, 8 November 2013 at 04:38:23 UTC, Ross Hays wrote: Thank you No problem. I'm glad we're making progress on it. And don't feel bad about mistakes while learning. They happen. Embrace them because they happen to all of us at first especially when we're juggling learning new syntaxes

Re: Template class with dispatched properties

2013-11-07 Thread Ross Hays
Sorry, I forgot to mention in that post that you have "toOffset" in your template constraint, which means it will also never match. You'll have to define it or replace it with `fieldName[0] - 'x';` Also, you might not want to do `fieldName[0 .. 1]` because that's a slice (which is just anothe

Re: Template class with dispatched properties

2013-11-07 Thread Chris Cain
On Friday, 8 November 2013 at 04:28:31 UTC, Ross Hays wrote: class Vector(int N, T) if (N <= 3) { T[N] data; this() { data[] = 0; } @property ref T opDispatch(string fieldName, Args ...)(Args args) if (Args.length < 2 && fieldName.length == 1 && toOffset(f

Re: Template class with dispatched properties

2013-11-07 Thread Ross Hays
On Friday, 8 November 2013 at 04:28:31 UTC, Ross Hays wrote: Strange. I'm getting a different error, but I'm still running 2.063.2. The error I get is `Error: cannot resolve type for t.opDispatch!("x")` What version are you running? I just updated to 2.064.2 In any case, the reason apparentl

Re: Template class with dispatched properties

2013-11-07 Thread Ross Hays
Strange. I'm getting a different error, but I'm still running 2.063.2. The error I get is `Error: cannot resolve type for t.opDispatch!("x")` What version are you running? I just updated to 2.064.2 In any case, the reason apparently is multifold: 1. Apparently the proper error message isn't s

Re: Template class with dispatched properties

2013-11-07 Thread Chris Cain
On Friday, 8 November 2013 at 04:06:22 UTC, Chris Cain wrote: So fix 2 and 3 and it works for getting x. Also, define `toOffset` in the template constraint.

Re: Template class with dispatched properties

2013-11-07 Thread Chris Cain
On Friday, 8 November 2013 at 03:42:12 UTC, Ross Hays wrote: I am actually a little curious why my original approach did not work at all. Using some of what you provided and some of what I had I get the following: import std.stdio; import std.string; class Vector(int N, T) if (N <= 3) { T

Re: Template class with dispatched properties

2013-11-07 Thread Chris Cain
On Friday, 8 November 2013 at 03:35:34 UTC, Ross Hays wrote: Awesome that seems to do what I was going for. I had tried a similar approach with @property dispatch and the subtraction of 'x', but I had left out the static if and had the opDispatch returning a ref of the entry in the array (so th

Re: Template class with dispatched properties

2013-11-07 Thread Ross Hays
I am actually a little curious why my original approach did not work at all. Using some of what you provided and some of what I had I get the following: import std.stdio; import std.string; class Vector(int N, T) if (N <= 3) { T[N] data; this() { data[] = 0; } @pr

Re: Template class with dispatched properties

2013-11-07 Thread Ross Hays
Awesome that seems to do what I was going for. I had tried a similar approach with @property dispatch and the subtraction of 'x', but I had left out the static if and had the opDispatch returning a ref of the entry in the array (so there would just be the one @property still) but that resulted

Re: Template class with dispatched properties

2013-11-07 Thread Chris Cain
On Friday, 8 November 2013 at 02:48:31 UTC, Chris Cain wrote: Minor tweaks might be necessary, but that should get you started. Actually, I refactored it a little bit to make it better (original code was just a bit too messy for my taste): --- struct Vector(int N, T) if (N <= 3) { private

Re: Template class with dispatched properties

2013-11-07 Thread Chris Cain
On Friday, 8 November 2013 at 02:13:01 UTC, Ross Hays wrote: My end goal is to be able to instantiate a vector with a syntax like... `Vector!(2, float) vec = new Vector!(2, float)();` ... Any suggestions? Greetings, This works: --- import std.stdio; struct Vector(int N, T) if (N <= 3) {

Template class with dispatched properties

2013-11-07 Thread Ross Hays
I have been playing around with a vector implementation that I am trying to write in D, and have been having problems getting something to work (it probably isn't even possible). My end goal is to be able to instantiate a vector with a syntax like... `Vector!(2, float) vec = new Vector!(2, float)