Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-04 Thread Gary Willoughby
In D is it possible to handle accessing class properties that don't exist, similar to PHP's magic methods? http://www.php.net/manual/en/language.oop5.overloading.php#object.set For example, the below class doesn't have the 'x' property: class T { this() { this.x = "hello world!

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-04 Thread Gary Willoughby
On Saturday, 4 January 2014 at 19:08:45 UTC, Gary Willoughby wrote: In D is it possible to handle accessing class properties that don't exist, similar to PHP's magic methods? http://www.php.net/manual/en/language.oop5.overloading.php#object.set For example, the below class doesn't have the 'x'

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-04 Thread Gary Willoughby
On Saturday, 4 January 2014 at 19:17:31 UTC, Gary Willoughby wrote: OMG i've just found opDispatch! http://dlang.org/operatoroverloading.html#Dispatch Fantastic! How to handle returning a value from a non-existent property?

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-04 Thread Gary Willoughby
On Saturday, 4 January 2014 at 19:19:43 UTC, Gary Willoughby wrote: On Saturday, 4 January 2014 at 19:17:31 UTC, Gary Willoughby wrote: OMG i've just found opDispatch! http://dlang.org/operatoroverloading.html#Dispatch Fantastic! How to handle returning a value from a non-existent property?

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-04 Thread Adam D. Ruppe
On Saturday, 4 January 2014 at 19:26:50 UTC, Gary Willoughby wrote: Got it! opDispatch rox, and there's all kinds of crazy stuff you can do with it. In my dom.d, I used it for three things: https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/dom.d

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-04 Thread H. S. Teoh
On Sat, Jan 04, 2014 at 07:52:11PM +, Adam D. Ruppe wrote: > On Saturday, 4 January 2014 at 19:26:50 UTC, Gary Willoughby wrote: > >Got it! > > opDispatch rox, and there's all kinds of crazy stuff you can do with > it. In my dom.d, I used it for three things: [...] Somebody has also used opDi

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-05 Thread Philippe Sigaud
On Sat, Jan 4, 2014 at 10:08 PM, H. S. Teoh wrote: > Of course, these are arguably clever hacks than true, properly-motivated > examples, but still, they exemplify what Andrei meant when he said that > the power of opDispatch is largely still unexplored territory. As Adam showed, it's very nice

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-05 Thread TheFlyingFiddle
Another simple example that have helped me tremendously when debugging OpenGL calls. A simple dispatcher that checks glGetError after every call. struct GL { auto opDispatch(string name, Args...)(Args args) { enum glName = "gl" ~ name; mixin(format(" static if(is

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-05 Thread Jacob Carlborg
On 2014-01-05 14:09, Philippe Sigaud wrote: As Adam showed, it's very nice to make some clean API (or DSL). Another example I like is generating queries: auto result = table.findByFirstName; If Table has a "FirstName" field, then opDispatch will catch any findByX and generate the related

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-05 Thread Artur Skawina
On 01/05/14 15:36, TheFlyingFiddle wrote: > Another simple example that have helped me tremendously when debugging OpenGL > calls. A simple dispatcher that checks glGetError after every call. > > struct GL > { > auto opDispatch(string name, Args...)(Args args) > { > enum glName =

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-05 Thread TheFlyingFiddle
On Sunday, 5 January 2014 at 17:17:27 UTC, Artur Skawina wrote: While 'void' is not a first class type in D, there /is/ a special case for returning 'void' from functions - so all of the above can simply be written as: struct gl { static auto ref opDispatch(string name, Args...)(Args

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-05 Thread Philippe Sigaud
On Sun, Jan 5, 2014 at 5:18 PM, Jacob Carlborg wrote: > Just for the record. In Rails, that's the old, now discourage, Rails 2 > syntax. I didn't know that, thanks. I read it during the holidays in Martin Fowler's book on DSL, but indeed that book is from 2005, IIRC. > In Rails 3 and later the

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-06 Thread Jacob Carlborg
On 2014-01-05 22:44, Philippe Sigaud wrote: I didn't know that, thanks. I read it during the holidays in Martin Fowler's book on DSL, but indeed that book is from 2005, IIRC. That's a bit old :). According to this site[1] Rails 1.0 was released in December 2005. Rails 4.0 was released in June

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-06 Thread Philippe Sigaud
>> I didn't know that, thanks. I read it during the holidays in Martin >> Fowler's book on DSL, but indeed that book is from 2005, IIRC. > > > That's a bit old :). According to this site[1] Rails 1.0 was released in > December 2005. Rails 4.0 was released in June 2013. Ouch, that was 2010, my bad.

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-06 Thread Jacob Carlborg
On 2014-01-06 22:07, Philippe Sigaud wrote: Ruby does have a clean syntax (though I find blocks to be a bit heavy). I like the block syntax. It allows one to create what looks like new statements: loop do # endless loop end I would like to have that in D as well, but with braces instead:

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-07 Thread Philippe Sigaud
On Tue, Jan 7, 2014 at 8:50 AM, Jacob Carlborg wrote: > I would like to have that in D as well, but with braces instead: > > void loop (void delegate () dg); > > loop { > // endless loop > } What about: void loop(void delegate() dg); loop({ ... }); Since any block is a void delegate().

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-07 Thread Jacob Carlborg
On 2014-01-07 13:22, Philippe Sigaud wrote: What about: void loop(void delegate() dg); loop({ ... }); Since any block is a void delegate(). That's what we have now, and that doesn't look like a built-in statement ;) -- /Jacob Carlborg

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-07 Thread H. S. Teoh
On Tue, Jan 07, 2014 at 03:35:43PM +0100, Jacob Carlborg wrote: > On 2014-01-07 13:22, Philippe Sigaud wrote: > > >What about: > > > >void loop(void delegate() dg); > > > >loop({ > >... > > > >}); > > > >Since any block is a void delegate(). > > That's what we have now, and that doesn't look like

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-07 Thread Jacob Carlborg
On 2014-01-07 16:58, H. S. Teoh wrote: Y'know, I've always wanted "trailing delegate syntax": func(x, y, z; p, q, r) { // body } gets translated into: func(p, q, r, (x, y, z) => /* body */); Since we already have UFCS, which translates a leading fragme

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-07 Thread H. S. Teoh
On Tue, Jan 07, 2014 at 09:18:48PM +0100, Jacob Carlborg wrote: > On 2014-01-07 16:58, H. S. Teoh wrote: > > >Y'know, I've always wanted "trailing delegate syntax": > > > > func(x, y, z; p, q, r) { > > // body > > } > > > >gets translated into: > > > > func(p, q, r, (x, y,

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-07 Thread Jacob Carlborg
On 2014-01-07 21:44, H. S. Teoh wrote: If you have a good motivating use case in favor of this addition that can be used in a DIP, I'd vote for it. I'm usually not good at these arguments. I mean, it would be nice to have but I don't have any strong arguments for it. It's just syntax sugar.

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-08 Thread H. S. Teoh
On Wed, Jan 08, 2014 at 08:32:15AM +0100, Jacob Carlborg wrote: > On 2014-01-07 21:44, H. S. Teoh wrote: [...] > >I like the alias idea, so here's the revised proposal: > > > >1) Argumentless trailing-delegate syntax: > > > > // Given this declaration: > > void foo(alias dg)(); > > > >

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-09 Thread Jacob Carlborg
On 2014-01-08 19:04, H. S. Teoh wrote: The reason I wrote it this way is so that it parallels the foreach construction better: my_foreach (i; range) { ... } parallels: foreach (i; range) { ... } I guessed that. Keep in mind t

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-09 Thread H. S. Teoh
On Thu, Jan 09, 2014 at 09:49:17AM +0100, Jacob Carlborg wrote: > On 2014-01-08 19:04, H. S. Teoh wrote: [...] > >In fact, we can already almost get the desired syntax in the current > >language: > > > > /* Current D already supports this: */ > > range.my_foreach!((i,j) { > > /*

Re: Is it possible to handle 'magic' property assignments a'la PHP?

2014-01-09 Thread Jacob Carlborg
On 2014-01-09 18:57, H. S. Teoh wrote: I'm afraid that this might become ambiguous, for example: int* gun(...) {...} func (x==0) *gun(y); Does the second statement mean `func!(() => *gun(y))(x==0)`, or does it mean `func(x==0) * gun(y)`? While it's not hard to