Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
March 24, 2007 opIndexCall | ||||
---|---|---|---|---|
| ||||
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? |
March 24, 2007 Re: opIndexCall | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dan | 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 |
March 24, 2007 Re: opIndexCall | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dan | 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
|
March 25, 2007 Re: opIndexCall | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | 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 |
March 25, 2007 Re: opIndexCall | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dan | "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. : p Uhh, 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? |
March 25, 2007 Re: opIndexCall | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dan | 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 |
March 25, 2007 Re: opIndexCall | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley Wrote:
> "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. : p
>
> Uhh, 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?
>
>
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.
|
Copyright © 1999-2021 by the D Language Foundation