Re: Extend the call site default argument expansion mechanism?

2018-05-16 Thread Nick Treleaven via Digitalmars-d
On Tuesday, 15 May 2018 at 15:02:36 UTC, jmh530 wrote: auto opDispatch(string s)() if (s == "bar") In case anyone isn't aware, this is better written: auto opDispatch(string s : "bar")()

Re: Extend the call site default argument expansion mechanism?

2018-05-16 Thread Simen Kjærås via Digitalmars-d
On Wednesday, 16 May 2018 at 10:51:51 UTC, jmh530 wrote: On Wednesday, 16 May 2018 at 09:01:29 UTC, Simen Kjærås wrote: snip] struct Foo(int x) { int n = x; auto opDispatch(string s)() if (s == "bar") { n++; return n;

Re: Extend the call site default argument expansion mechanism?

2018-05-16 Thread jmh530 via Digitalmars-d
On Wednesday, 16 May 2018 at 09:01:29 UTC, Simen Kjærås wrote: snip] struct Foo(int x) { int n = x; auto opDispatch(string s)() if (s == "bar") { n++; return n; } } unittest { int y = 0;

Re: Extend the call site default argument expansion mechanism?

2018-05-16 Thread Simen Kjærås via Digitalmars-d
On Tuesday, 15 May 2018 at 15:02:36 UTC, jmh530 wrote: On Tuesday, 15 May 2018 at 14:52:46 UTC, Steven Schveighoffer wrote: [snip] It seems opDispatch isn't being used in the with statement. That seems like a bug, or maybe a limitation. I'm not sure how "with" works, but I assumed it would

Re: Extend the call site default argument expansion mechanism?

2018-05-15 Thread Computermatronic via Digitalmars-d
On Tuesday, 15 May 2018 at 20:31:14 UTC, jmh530 wrote: On Tuesday, 15 May 2018 at 15:02:36 UTC, jmh530 wrote: [snip] Note, it's not an issue if Foo were not a struct. This was fixed in Bug 6400 [1]l. The issue is with template instances. I have filed a new enhancement request [2] [1]

Re: Extend the call site default argument expansion mechanism?

2018-05-15 Thread jmh530 via Digitalmars-d
On Tuesday, 15 May 2018 at 15:02:36 UTC, jmh530 wrote: [snip] Note, it's not an issue if Foo were not a struct. This was fixed in Bug 6400 [1]l. The issue is with template instances. I have filed a new enhancement request [2] [1] https://issues.dlang.org/show_bug.cgi?id=6400 [2]

Re: Extend the call site default argument expansion mechanism?

2018-05-15 Thread jmh530 via Digitalmars-d
On Tuesday, 15 May 2018 at 14:52:46 UTC, Steven Schveighoffer wrote: [snip] It seems opDispatch isn't being used in the with statement. That seems like a bug, or maybe a limitation. I'm not sure how "with" works, but I assumed it would try calling as a member, and then if it doesn't work,

Re: Extend the call site default argument expansion mechanism?

2018-05-15 Thread Meta via Digitalmars-d
On Tuesday, 15 May 2018 at 14:52:46 UTC, Steven Schveighoffer wrote: Sadly with(WithAlloc!alloc) doesn't work. (If you have to use withAlloc.func everywhere, it kind of destroy the point, doesn't it?) It seems opDispatch isn't being used in the with statement. That seems like a bug, or maybe

Re: Extend the call site default argument expansion mechanism?

2018-05-15 Thread Steven Schveighoffer via Digitalmars-d
On 5/15/18 10:26 AM, Yuxuan Shui wrote: On Tuesday, 15 May 2018 at 13:59:37 UTC, jmh530 wrote: On Tuesday, 15 May 2018 at 13:16:21 UTC, Steven Schveighoffer wrote: [snip] Hm... neat idea. Somehow, opDispatch can probably be used to make this work even more generically (untested): struct

Re: Extend the call site default argument expansion mechanism?

2018-05-15 Thread jmh530 via Digitalmars-d
On Tuesday, 15 May 2018 at 14:26:48 UTC, Yuxuan Shui wrote: [snip] Example: https://run.dlang.io/is/RV2xIH Sadly with(WithAlloc!alloc) doesn't work. (If you have to use withAlloc.func everywhere, it kind of destroy the point, doesn't it?) Yeah I know, I tried it, but couldn't figure out

Re: Extend the call site default argument expansion mechanism?

2018-05-15 Thread Yuxuan Shui via Digitalmars-d
On Tuesday, 15 May 2018 at 13:59:37 UTC, jmh530 wrote: On Tuesday, 15 May 2018 at 13:16:21 UTC, Steven Schveighoffer wrote: [snip] Hm... neat idea. Somehow, opDispatch can probably be used to make this work even more generically (untested): struct WithAlloc(alias alloc) { auto

Re: Extend the call site default argument expansion mechanism?

2018-05-15 Thread jmh530 via Digitalmars-d
On Tuesday, 15 May 2018 at 13:16:21 UTC, Steven Schveighoffer wrote: [snip] Hm... neat idea. Somehow, opDispatch can probably be used to make this work even more generically (untested): struct WithAlloc(alias alloc) { auto opDispatch(string s, Args...)(auto ref Args args) if

Re: Extend the call site default argument expansion mechanism?

2018-05-15 Thread Steven Schveighoffer via Digitalmars-d
On 5/15/18 7:53 AM, Yuxuan Shui wrote: On Friday, 11 May 2018 at 18:55:03 UTC, Meta wrote: On Friday, 11 May 2018 at 15:03:41 UTC, Uknown wrote: [...] It's not as pretty, and I don't know if it works outside this toy example yet, but you can do: import std.stdio; struct Allocator {    

Re: Extend the call site default argument expansion mechanism?

2018-05-15 Thread Atila Neves via Digitalmars-d
On Friday, 11 May 2018 at 13:22:12 UTC, Meta wrote: On Friday, 11 May 2018 at 11:42:07 UTC, Dukc wrote: On Thursday, 10 May 2018 at 14:15:18 UTC, Yuxuan Shui wrote: ... // constructor of DataStructure this(Allocator alloc=__ALLOC__) {...} ... auto alloc = new SomeAllocator(); define __ALLOC__

Re: Extend the call site default argument expansion mechanism?

2018-05-15 Thread Yuxuan Shui via Digitalmars-d
On Friday, 11 May 2018 at 18:55:03 UTC, Meta wrote: On Friday, 11 May 2018 at 15:03:41 UTC, Uknown wrote: [...] It's not as pretty, and I don't know if it works outside this toy example yet, but you can do: import std.stdio; struct Allocator { auto call(alias F, Args...)(Args args)

Re: Extend the call site default argument expansion mechanism?

2018-05-11 Thread jmh530 via Digitalmars-d
On Friday, 11 May 2018 at 11:42:07 UTC, Dukc wrote: [snip] Doesn't this basically mean including the implicits Martin Odersky talked about at Dconf in D? I don't know whether it's a good idea all-in-all, but assuming the arguments can be used as compile-time I can already see a big use

Re: Extend the call site default argument expansion mechanism?

2018-05-11 Thread Yuxuan Shui via Digitalmars-d
On Thursday, 10 May 2018 at 15:15:03 UTC, Paul Backus wrote: On Thursday, 10 May 2018 at 14:37:00 UTC, rikki cattermole wrote: On 11/05/2018 2:33 AM, Yuxuan Shui wrote: On Thursday, 10 May 2018 at 14:28:39 UTC, JN wrote: But doing it with default argument expansion saves you 1 allocation,

Re: Extend the call site default argument expansion mechanism?

2018-05-11 Thread Meta via Digitalmars-d
On Friday, 11 May 2018 at 15:03:41 UTC, Uknown wrote: I see what you're saying and I agree with you. I think a better way would be to try and extend the `with` syntax to work with arbitrary functions, rather than only objects. That would make it more useful. So something like: --- void

Re: Extend the call site default argument expansion mechanism?

2018-05-11 Thread Uknown via Digitalmars-d
On Friday, 11 May 2018 at 14:26:21 UTC, Jonathan M Davis wrote: On Thursday, May 10, 2018 14:15:18 Yuxuan Shui via Digitalmars-d wrote: So in D I can use default argument like this: [...] Is this a good idea? It seems like really risky move, honestly, because it means that the function is

Re: Extend the call site default argument expansion mechanism?

2018-05-11 Thread Jonathan M Davis via Digitalmars-d
On Thursday, May 10, 2018 14:15:18 Yuxuan Shui via Digitalmars-d wrote: > So in D I can use default argument like this: > > int f(int line=__LINE__) {} > > And because default argument is expanded at call site, f() will > be called with the line number of the call site. > > This is a really clever

Re: Extend the call site default argument expansion mechanism?

2018-05-11 Thread Timon Gehr via Digitalmars-d
On 10.05.2018 16:22, rikki cattermole wrote: On 11/05/2018 2:20 AM, Yuxuan Shui wrote: On Thursday, 10 May 2018 at 14:17:50 UTC, rikki cattermole wrote: On 11/05/2018 2:15 AM, Yuxuan Shui wrote: [...] Bad idea, too much magic. This magic is already there in D. I just want to use it in a

Re: Extend the call site default argument expansion mechanism?

2018-05-11 Thread Meta via Digitalmars-d
On Friday, 11 May 2018 at 11:42:07 UTC, Dukc wrote: On Thursday, 10 May 2018 at 14:15:18 UTC, Yuxuan Shui wrote: ... // constructor of DataStructure this(Allocator alloc=__ALLOC__) {...} ... auto alloc = new SomeAllocator(); define __ALLOC__ = alloc; // And we don't need to pass alloc everytime

Re: Extend the call site default argument expansion mechanism?

2018-05-11 Thread Dukc via Digitalmars-d
On Thursday, 10 May 2018 at 14:15:18 UTC, Yuxuan Shui wrote: ... // constructor of DataStructure this(Allocator alloc=__ALLOC__) {...} ... auto alloc = new SomeAllocator(); define __ALLOC__ = alloc; // And we don't need to pass alloc everytime ... Is this a good idea? Doesn't this basically

Re: Extend the call site default argument expansion mechanism?

2018-05-10 Thread Paul Backus via Digitalmars-d
On Thursday, 10 May 2018 at 14:37:00 UTC, rikki cattermole wrote: On 11/05/2018 2:33 AM, Yuxuan Shui wrote: On Thursday, 10 May 2018 at 14:28:39 UTC, JN wrote: But doing it with default argument expansion saves you 1 allocation, has 1 less type, while being just as readable. I think that's a

Re: Extend the call site default argument expansion mechanism?

2018-05-10 Thread Uknown via Digitalmars-d
On Thursday, 10 May 2018 at 14:37:00 UTC, rikki cattermole wrote: On 11/05/2018 2:33 AM, Yuxuan Shui wrote: On Thursday, 10 May 2018 at 14:28:39 UTC, JN wrote: On Thursday, 10 May 2018 at 14:15:18 UTC, Yuxuan Shui wrote: [...] But doing it with default argument expansion saves you 1

Re: Extend the call site default argument expansion mechanism?

2018-05-10 Thread rikki cattermole via Digitalmars-d
On 11/05/2018 2:33 AM, Yuxuan Shui wrote: On Thursday, 10 May 2018 at 14:28:39 UTC, JN wrote: On Thursday, 10 May 2018 at 14:15:18 UTC, Yuxuan Shui wrote: [...] For things like this you can use the OOP Factory pattern, pseudocode: class DataStructureFactory {   this(Allocator alloc)   {    

Re: Extend the call site default argument expansion mechanism?

2018-05-10 Thread Yuxuan Shui via Digitalmars-d
On Thursday, 10 May 2018 at 14:30:49 UTC, Seb wrote: On Thursday, 10 May 2018 at 14:15:18 UTC, Yuxuan Shui wrote: So in D I can use default argument like this: int f(int line=__LINE__) {} [...] Why not define a TLS or global variable like theAllocator? Or if you know it at compile-time as

Re: Extend the call site default argument expansion mechanism?

2018-05-10 Thread Yuxuan Shui via Digitalmars-d
On Thursday, 10 May 2018 at 14:28:39 UTC, JN wrote: On Thursday, 10 May 2018 at 14:15:18 UTC, Yuxuan Shui wrote: [...] For things like this you can use the OOP Factory pattern, pseudocode: class DataStructureFactory { this(Allocator alloc) { this.alloc = alloc; } Allocator

Re: Extend the call site default argument expansion mechanism?

2018-05-10 Thread Seb via Digitalmars-d
On Thursday, 10 May 2018 at 14:15:18 UTC, Yuxuan Shui wrote: So in D I can use default argument like this: int f(int line=__LINE__) {} [...] Why not define a TLS or global variable like theAllocator? Or if you know it at compile-time as an alias?

Re: Extend the call site default argument expansion mechanism?

2018-05-10 Thread JN via Digitalmars-d
On Thursday, 10 May 2018 at 14:15:18 UTC, Yuxuan Shui wrote: So in D I can use default argument like this: int f(int line=__LINE__) {} And because default argument is expanded at call site, f() will be called with the line number of the call site. This is a really clever feature, and I

Re: Extend the call site default argument expansion mechanism?

2018-05-10 Thread rikki cattermole via Digitalmars-d
On 11/05/2018 2:20 AM, Yuxuan Shui wrote: On Thursday, 10 May 2018 at 14:17:50 UTC, rikki cattermole wrote: On 11/05/2018 2:15 AM, Yuxuan Shui wrote: [...] Bad idea, too much magic. This magic is already there in D. I just want to use it in a different way. The magic is not already in

Re: Extend the call site default argument expansion mechanism?

2018-05-10 Thread Yuxuan Shui via Digitalmars-d
On Thursday, 10 May 2018 at 14:17:50 UTC, rikki cattermole wrote: On 11/05/2018 2:15 AM, Yuxuan Shui wrote: [...] Bad idea, too much magic. This magic is already there in D. I just want to use it in a different way.

Re: Extend the call site default argument expansion mechanism?

2018-05-10 Thread rikki cattermole via Digitalmars-d
On 11/05/2018 2:15 AM, Yuxuan Shui wrote: So in D I can use default argument like this: int f(int line=__LINE__) {} And because default argument is expanded at call site, f() will be called with the line number of the call site. This is a really clever feature, and I think a similar feature