www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Uniform function call syntax

reply Jacob Carlborg <doob me.com> writes:
In the first D conference there was some talk about uniform function 
call syntax which allowed a.foo(x) and foo(a, x) to be interchangeable 
(just as we have now with arrays), what happened do that? Will it still 
happen?

Except for the mentioned advantages in the talk I've found another use 
for it. When creating bindings to Objective-C it could be used to 
simulate categories which otherwise is quite hard simulate and doesn't 
scale well. The only way I found requires that the source is available 
of the class to add the methods to.

I poked around in the DMD sources I found that it's really easy to add, 
just edit one line. However there is a problem it don't work for 
literals like 3.foo(), for that I think that parser needs to be modified.


For those unfamiliar with Objective-C and categories: "A category allows 
you to add methods to an existing class—even to one to which you do not 
have the source", 



/Jacob Carlborg
Oct 08 2009
next sibling parent reply "Nick Sabalausky" <a a.a> writes:
"Jacob Carlborg" <doob me.com> wrote in message 
news:halgto$1kc0$1 digitalmars.com...
 In the first D conference there was some talk about uniform function call 
 syntax which allowed a.foo(x) and foo(a, x) to be interchangeable (just as 
 we have now with arrays), what happened do that? Will it still happen?
Yea, I find not having that to be a painful inconsistancy. Been hoping for that for a while.
 Except for the mentioned advantages in the talk I've found another use for 
 it. When creating bindings to Objective-C it could be used to simulate 
 categories which otherwise is quite hard simulate and doesn't scale well. 
 The only way I found requires that the source is available of the class to 
 add the methods to.

 I poked around in the DMD sources I found that it's really easy to add, 
 just edit one line.
Can you please post the file, line number, old line, new line, and dmd ver? Or submit a patch to bugzilla. That could be helpful for anyone who wants to look into this furthur.
 However there is a problem it don't work for literals like 3.foo(), for 
 that I think that parser needs to be modified.


 For those unfamiliar with Objective-C and categories: "A category allows 
 you to add methods to an existing class—even to one to which you do not 
 have the source", 

Do you know if that actually affects the class itself (like adds an entry to calls extension methods, which are just like what D does with arrays (except that you have to actually declare the function to be an extension method - which I've been kind of on the fence about as to whether I like that or not).
Oct 08 2009
next sibling parent Michel Fortin <michel.fortin michelf.com> writes:
On 2009-10-08 18:23:32 -0400, "Nick Sabalausky" <a a.a> said:

 However there is a problem it don't work for literals like 3.foo(), for
 that I think that parser needs to be modified.
 
 
 For those unfamiliar with Objective-C and categories: "A category allows
 you to add methods to an existing class—even to one to which you do not
 have the source",

Do
 
you know if that actually affects the class itself (like adds an entry to calls extension methods, which are just like what D does with arrays (except that you have to actually declare the function to be an extension method - which I've been kind of on the fence about as to whether I like that or not).
There is no vtable in Objective-C, but yes categories will add methods to classes just as if they where in the original class. Methods from categories are overridable in subclasses and can be seen through reflection. I've proposed previously a similar system for D. The idea was to build vtables at runtime, or use a special linker to do the same. See: http://michelf.com/weblog/2009/some-ideas-for-dynamic-vtables-in-d/ and: http://michelf.com/weblog/2008/vtable-benchmarking/ -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Oct 08 2009
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 10/9/09 00:23, Nick Sabalausky wrote:
 "Jacob Carlborg"<doob me.com>  wrote in message
 news:halgto$1kc0$1 digitalmars.com...
 In the first D conference there was some talk about uniform function call
 syntax which allowed a.foo(x) and foo(a, x) to be interchangeable (just as
 we have now with arrays), what happened do that? Will it still happen?
Yea, I find not having that to be a painful inconsistancy. Been hoping for that for a while.
 Except for the mentioned advantages in the talk I've found another use for
 it. When creating bindings to Objective-C it could be used to simulate
 categories which otherwise is quite hard simulate and doesn't scale well.
 The only way I found requires that the source is available of the class to
 add the methods to.

 I poked around in the DMD sources I found that it's really easy to add,
 just edit one line.
Can you please post the file, line number, old line, new line, and dmd ver? Or submit a patch to bugzilla. That could be helpful for anyone who wants to look into this furthur.
I've submitted a patch to bugzilla: http://d.puremagic.com/issues/show_bug.cgi?id=3382
 However there is a problem it don't work for literals like 3.foo(), for
 that I think that parser needs to be modified.


 For those unfamiliar with Objective-C and categories: "A category allows
 you to add methods to an existing class—even to one to which you do not
 have the source",

Do you know if that actually affects the class itself (like adds an entry to calls extension methods, which are just like what D does with arrays (except that you have to actually declare the function to be an extension method - which I've been kind of on the fence about as to whether I like that or not).
Oct 09 2009
prev sibling parent reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
Jacob Carlborg wrote:

 I poked around in the DMD sources I found that it's really easy to add,
 just edit one line. However there is a problem it don't work for
 literals like 3.foo(), for that I think that parser needs to be modified.
 
You'd have to modify a bit more than the parser. ["3.f",<9>,line=2,col=9] ["oo",<4>,line=2,col=12] ["(",<146>,line=2,col=14] [")",<147>,line=2,col=15] [";",<154>,line=2,col=16] Otherwise, the parser can handle that as is. Surround the 3 with parens.
Oct 08 2009
parent reply "Nick Sabalausky" <a a.a> writes:
"Ellery Newcomer" <ellery-newcomer utulsa.edu> wrote in message 
news:ham5lo$dq0$1 digitalmars.com...
 Jacob Carlborg wrote:

 I poked around in the DMD sources I found that it's really easy to add,
 just edit one line. However there is a problem it don't work for
 literals like 3.foo(), for that I think that parser needs to be modified.
You'd have to modify a bit more than the parser. ["3.f",<9>,line=2,col=9] ["oo",<4>,line=2,col=12] ["(",<146>,line=2,col=14] [")",<147>,line=2,col=15] [";",<154>,line=2,col=16] Otherwise, the parser can handle that as is. Surround the 3 with parens.
Yet another example why "1." and ".1" literals are more trouble than they're worth.
Oct 08 2009
parent Kagamin <spam here.lot> writes:
Nick Sabalausky Wrote:

 Yet another example why "1." and ".1" literals are more trouble than they're 
 worth.
They save you 1 keystroke :3
Oct 09 2009