digitalmars.D.learn - Using opDispatch as *magic* getter/setter. Possible or not?
- =?UTF-8?B?QWxla3NhbmRhciBSdcW+acSNacSH?= (10/10) Mar 30 2011 Is it possible to use opDispatch as generic getter and setter at the
- Jacob Carlborg (28/38) Mar 30 2011 You can't have two methods named opDispatch like that. You can do
- =?UTF-8?B?QWxla3NhbmRhciBSdcW+acSNacSH?= (10/33) Mar 31 2011 That's great! Thanks! If just the assignment worked thought, it would
- Steven Schveighoffer (12/49) Mar 31 2011 The issue is that you can't have two templates with the same exact
- =?UTF-8?B?QWxla3NhbmRhciBSdcW+acSNacSH?= (23/27) Mar 31 2011 Thanks, that's much more readable I now have these templates:
- Kagamin (2/6) Mar 31 2011 If two template instances have the same exact template parameters, it's ...
- Steven Schveighoffer (18/25) Mar 31 2011 Yes, but we allow function overloading. Problem is, you can't do
- Simen kjaeraas (13/23) Apr 04 2011 Bug 620[1] is related to what you ask. I have written a solution there,
- =?UTF-8?B?QWxla3NhbmRhciBSdcW+acSNacSH?= (3/14) Apr 05 2011 Thank you, I will try your workaround.
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 than one template declaration, path\to\test.d(57):opDispatch(string entryName) and path\to\test.d(66):opDispatch(string entryName) Is mixing property with opDispatch supposed to work at all or is opDispatch only ment for method calls? Or maybe there is some other way to achive what I want and I'm not aware of it? :-)
Mar 30 2011
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 than one template declaration, path\to\test.d(57):opDispatch(string entryName) and path\to\test.d(66):opDispatch(string entryName) Is mixing property with opDispatch supposed to work at all or is opDispatch only ment for method calls? Or maybe there is some other way to achive what I want and I'm not aware of it? :-)You can't have two methods named opDispatch like that. You can do something like this: class Foo { property ref ConfigSection opDispatch(string sectionName, Args ...)(Args args) { static if (Args.length == 0) {} // getter else static if (Args.length == 1) { auto value = args[0]; } // setter } 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 ...)(Args args) if (Args.length == 1) { // setter } } auto foo = new Foo; foo.bar; // works foo.bar = 3; // currently does not work, bug foo.bar(3); // works -- /Jacob Carlborg
Mar 30 2011
On Thu, Mar 31, 2011 at 8:52 AM, Jacob Carlborg <doob 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 ...)(Args args) if (Args.length == 1) { // setter } } auto foo = new Foo; foo.bar; // works foo.bar = 3; // currently does not work, bug foo.bar(3); // works -- /Jacob CarlborgThat's great! Thanks! If just the assignment worked thought, it would be perfect.. that bug but can't find it on bugzilla. On Thu, Mar 31, 2011 at 3:50 AM, spir <denis.spir gmail.com> wrote:Agreed. And I would really have an answer to your question, since I tried to do the same thing. Don't understand why D does not have an 'opMember' or 'opDot'. Someone knows? This would be one of the first metamethods I would introduce in a language (definitely before operator overloading).Yeah, opMember would be great, but it seems that we'll be able to use opDispatch for that in a way Jacob described once the bugs are ironed out.. But until then I'll have to live with "ugly" indexing sytnax...
Mar 31 2011
On Thu, 31 Mar 2011 02:52:15 -0400, Jacob Carlborg <doob me.com> wrote:On 3/31/11 2:32 AM, Aleksandar Ružičić 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, then calls the function. I hope this restriction can be lifted for the special case of template functions. The ability to overload template functions is long overdue, especially when you are overloading with normal functions.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 than one template declaration, path\to\test.d(57):opDispatch(string entryName) and path\to\test.d(66):opDispatch(string entryName) Is mixing property with opDispatch supposed to work at all or is opDispatch only ment for method calls? Or maybe there is some other way to achive what I want and I'm not aware of it? :-)You can't have two methods named opDispatch like that. You can do something like this: class Foo { property ref ConfigSection opDispatch(string sectionName, Args ...)(Args args) { static if (Args.length == 0) {} // getter else static if (Args.length == 1) { auto value = args[0]; } // setter }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 ...)(Args args) if (Args.length == 1) { // setter } }or you can change the template parameters in the opDispatch setter: property ref ConfigSection opDispatch(string sectionName, T : string)(T arg) -Steve
Mar 31 2011
On Thu, Mar 31, 2011 at 1:30 PM, Steven Schveighoffer <schveiguy yahoo.com> wrote:or you can change the template parameters in the opDispatch setter: property ref ConfigSection opDispatch(string sectionName, T : string)(T arg) -SteveThanks, that's much more readable I now have these templates: // getter property ref ConfigEntry opDispatch(string entryName)() pure { if (!(entryName in entries)) { entries[entryName] = new ConfigEntry(config); } return entries[entryName]; } // setter property void opDispatch(string entryName, T : string)(in T value) { if (!(entryName in entries)) { entries[entryName] = new ConfigEntry(config); } entries[entryName] = value; } And that compiles just fine (I dunno if it runs ok since I'm currently on windows and I've been messing with my setup so optlink can't stop complaining.. sigh..) but due to bug not allowing foo.bar = "abc"; I've also added opIndex and opIndexAssign (apparently you cannot only have opIndexAssign defined) as temporary "backup" solution..
Mar 31 2011
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, then calls the function.If two template instances have the same exact template parameters, it's one instance, isn't it? There's no need to instantiate it twice.
Mar 31 2011
On Thu, 31 Mar 2011 14:02:43 -0400, Kagamin <spam here.lot> wrote:Steven Schveighoffer Wrote:Yes, but we allow function overloading. Problem is, you can't do *template* function overloading unless you do it on the template parameters. For instance, these two cannot be instantiated: void foo(string s)(int x) {writeln(x);} void foo(string s)(string y) {writeln(y);} because the compiler considers that you defined the same template twice with different implementations. However, if you do: void foo(string s, T)(T x) if (is(T == int)) { writeln(x); } void foo(string s, T)(T x) if (is(T == string)) { writeln(x); } then you have two different templates, and the compiler has no issue. This is a huge nuisance for operator overloading and opDispatch because the only required template parameter is the string of the operator/function name. I suspect that fixing this will be a large change in the compiler. -SteveThe 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, then calls the function.If two template instances have the same exact template parameters, it's one instance, isn't it? There's no need to instantiate it twice.
Mar 31 2011
On Thu, 31 Mar 2011 02:32:35 +0200, Aleksandar Ru=C5=BEi=C4=8Di=C4=87 = <ruzicic.aleksandar gmail.com> 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 than one template declaration, path\to\test.d(57):opDispatch(string entryName) and path\to\test.d(66):opDispatch(string entryName) Is mixing property with opDispatch supposed to work at all or is opDispatch only ment for method calls? Or maybe there is some other way to achive what I want and I'm not aware of it? :-)Bug 620[1] is related to what you ask. I have written a solution there, but I do not remember how well it works, and have no way of testing it currently: template opDispatch( string name ) { auto opDispatch( T... )( T args ) { // Do something! } } [1]: http://d.puremagic.com/issues/show_bug.cgi?id=3D620 -- = Simen
Apr 04 2011
On Tue, Apr 5, 2011 at 6:46 AM, Simen kjaeraas <simen.kjaras gmail.com> wro= te:Bug 620[1] is related to what you ask. I have written a solution there, but I do not remember how well it works, and have no way of testing it currently: template opDispatch( string name ) { =C2=A0 =C2=A0auto opDispatch( T... )( T args ) { =C2=A0 =C2=A0 =C2=A0 =C2=A0// Do something! =C2=A0 =C2=A0} } [1]: http://d.puremagic.com/issues/show_bug.cgi?id=3D620 -- SimenThank you, I will try your workaround.
Apr 05 2011