digitalmars.D - opIndexCall
- Dan (22/22) Mar 24 2007 I've recently discovered the want for an opIndexCall override in D.
- Chris Nicholson-Sauls (9/39) Mar 24 2007 I'm not sure, but it would help muchly to know the actual signatures/imp...
- Bill Baxter (7/38) Mar 24 2007 Does it work if you use a plain method instead of indexing? I.e. if you...
- Dan (6/6) Mar 24 2007 Heh...
- Jarrett Billingsley (32/42) Mar 24 2007 Uhh, well:
- Dan (3/63) Mar 25 2007 Okay, yeah. I wrote it out and it works. : p
- Derek Parnell (54/55) Mar 24 2007 It seems that all you need to do is get the return type of the opIndex
I've recently discovered the want for an opIndexCall override in D. For this: struct Y { ... Y function() f; } struct X { char[][] keys; Y[] values; opIndex() opIndexAssign() } Y myF(){ Y y; return y; } X z; z["hello"] = &myF; z["hello"](); // <-- can't do that. Y function() a; // have to do this? a = z["hello"]; // and this a(); // and this?
Mar 24 2007
Dan wrote:I've recently discovered the want for an opIndexCall override in D. For this: struct Y { ... Y function() f; } struct X { char[][] keys; Y[] values; opIndex() opIndexAssign() } Y myF(){ Y y; return y; } X z; z["hello"] = &myF; z["hello"](); // <-- can't do that.I'm not sure, but it would help muchly to know the actual signatures/implementations of opIndex and opIndexAssign. In my own little test, this worked with no issues. What error messages were you getting? And with what compiler version?Y function() a; // have to do this? a = z["hello"]; // and this a(); // and this?Actually, you could simplify it somewhat using auto. But in general this shouldn't be neccessary. auto a = z["hello"]; a(); -- Chris Nicholson-Sauls
Mar 24 2007
Dan wrote:I've recently discovered the want for an opIndexCall override in D. For this: struct Y { ... Y function() f; } struct X { char[][] keys; Y[] values; opIndex() opIndexAssign() } Y myF(){ Y y; return y; } X z; z["hello"] = &myF; z["hello"](); // <-- can't do that. Y function() a; // have to do this? a = z["hello"]; // and this a(); // and this?Does it work if you use a plain method instead of indexing? I.e. if you add a get_elem(char[] key), does get_elem("hello")() work? It sounds more like a bug to me than a need for an opIndexCall. Something seems fishy about your code, though. You have a Y[] array, but no array of Y function() anywhere. --bb
Mar 24 2007
Heh... the code was a sample, not my actual code (obviously) I was trying to only show enough to represent what I was trying to suggest. That said, I was hoping we could do: myAssocStruct[myCharArray](params) Which I've never seen done before in D in any examples or source online, or in the guide. I haven't actually tried a minimal test case, I just didn't know the feature existed at all and was suggesting we ought to be able to do so. If we can, then hey... I'll try to figure it out then. : p
Mar 24 2007
"Dan" <murpsoft hotmail.com> wrote in message news:eu4man$1cs3$1 digitalmars.com...Heh... the code was a sample, not my actual code (obviously) I was trying to only show enough to represent what I was trying to suggest. That said, I was hoping we could do: myAssocStruct[myCharArray](params) Which I've never seen done before in D in any examples or source online, or in the guide. I haven't actually tried a minimal test case, I just didn't know the feature existed at all and was suggesting we ought to be able to do so. If we can, then hey... I'll try to figure it out then. : pUhh, well: struct FuncHolder { void function(int)[char[]] funcs; void function(int) opIndex(char[] name) { return funcs[name]; } void opIndexAssign(void function(int) func, char[] name) { funcs[name] = func; } } void foo(int x) { writefln("foo: ", x); } void bar(int x) { writefln("bar: ", x); } void main() { FuncHolder fh; fh["foo"] = &foo; fh["bar"] = &bar; fh["foo"](3); fh["bar"](4); } Unless you're getting at something else?
Mar 24 2007
Jarrett Billingsley Wrote:"Dan" <murpsoft hotmail.com> wrote in message news:eu4man$1cs3$1 digitalmars.com...Okay, yeah. I wrote it out and it works. : p Guess there's no need for an opIndexCall then. Maybe just perhaps having something about this in documentation for the next guy.Heh... the code was a sample, not my actual code (obviously) I was trying to only show enough to represent what I was trying to suggest. That said, I was hoping we could do: myAssocStruct[myCharArray](params) Which I've never seen done before in D in any examples or source online, or in the guide. I haven't actually tried a minimal test case, I just didn't know the feature existed at all and was suggesting we ought to be able to do so. If we can, then hey... I'll try to figure it out then. : pUhh, well: struct FuncHolder { void function(int)[char[]] funcs; void function(int) opIndex(char[] name) { return funcs[name]; } void opIndexAssign(void function(int) func, char[] name) { funcs[name] = func; } } void foo(int x) { writefln("foo: ", x); } void bar(int x) { writefln("bar: ", x); } void main() { FuncHolder fh; fh["foo"] = &foo; fh["bar"] = &bar; fh["foo"](3); fh["bar"](4); } Unless you're getting at something else?
Mar 25 2007
On Sat, 24 Mar 2007 13:52:12 -0400, Dan wrote:I've recently discovered the want for an opIndexCall override in D.It seems that all you need to do is get the return type of the opIndex right. Here is my pretend example... import std.stdio; struct Y { Y function() f; } struct X { char[][] keys; Y[] values; // Has to return a function pointer. Y function() opIndex(char[] idx) { foreach(int i, char[] s; keys) { if (s == idx) return (values[i].f); } return null; } void opIndexAssign(Y function() fp, char[] idx) { foreach(int i, char[] s; keys) { if (s == idx) { values[i].f = fp; return; } } values.length = values.length + 1; (values[$-1].f) = fp; keys ~= idx; } } Y myF(){ Y y; writefln("Got it"); return y; } void main() { X z; z["hello"] = &myF; z["hello"](); // <-- No worries, mate. Y function() a; // have to do this? a = z["hello"]; // and this a(); // and this? } -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnell
Mar 24 2007