digitalmars.D - DIP 1011--extern(delegate)--Preliminary Review Round 1
- Mike Parker (12/12) Jul 14 2017 DIP 1011 is titled "extern(delegate)".
- Dominikus Dittes Scherkl (2/3) Jul 14 2017 Looks good to me, useful, doesn't brake code, DIP seems correct.
- Steven Schveighoffer (12/27) Jul 14 2017 It seems reasonable.
- Jonathan Marler (73/103) Jul 14 2017 I thought about this use case and it's an interesting one.
- Steven Schveighoffer (19/57) Jul 14 2017 I can say from experience, this can wreak havoc on things like template
- Nicholas Wilson (2/3) Jul 14 2017 Looks good!
- Dmitry Olshansky (8/10) Jul 14 2017 Contrary to other poster I believe this is wrong way to solve a
- Steven Schveighoffer (4/16) Jul 14 2017 But I think the idea is to avoid the extra indirection and call, no? We
- Jonathan Marler (3/5) Jul 14 2017 This proposal doesn't make ABI wrappers, it changes the ABI of
- Nicholas Wilson (3/15) Jul 16 2017 Not that it is a requirement to consider fir this DIP, but how
- Jonathan Marler (30/50) Jul 17 2017 I don't see any relation. Looks like the lambda capture lowers
- Nicholas Wilson (2/11) Jul 24 2017 Fair enough.
- Andrea Fontana (7/19) Jul 24 2017 I don't like it so much but also something like this could be
- Steven Schveighoffer (3/27) Jul 24 2017 I think you meant this for DIP 1009?
- Andrea Fontana (4/14) Jul 25 2017 You're right, I had too many windows opened :)
- =?UTF-8?B?c29vbGHDr21hbg==?= (38/38) Jul 21 2018 I know this one year old already but the DIP is still in formal
- Jonathan Marler (4/7) Jul 22 2018 This doesn't work because the ABI of a normal function is NOT THE
DIP 1011 is titled "extern(delegate)". https://github.com/dlang/DIPs/blob/master/DIPs/DIP1011.md All review-related feedback on and discussion of the DIP should occur in this thread. The review period will end at 11:59 PM ET on July 28 (3:59 AM GMT July 29), or when I make a post declaring it complete. At the end of Round 1, if further review is deemed necessary, the DIP will be scheduled for another round. Otherwise, it will be queued for the formal review and evaluation by the language authors. Thanks in advance to all who participate. Destroy!
Jul 14 2017
On Friday, 14 July 2017 at 10:43:05 UTC, Mike Parker wrote:DIP 1011 is titled "extern(delegate)".Looks good to me, useful, doesn't brake code, DIP seems correct.
Jul 14 2017
On 7/14/17 6:43 AM, Mike Parker wrote:DIP 1011 is titled "extern(delegate)". https://github.com/dlang/DIPs/blob/master/DIPs/DIP1011.md All review-related feedback on and discussion of the DIP should occur in this thread. The review period will end at 11:59 PM ET on July 28 (3:59 AM GMT July 29), or when I make a post declaring it complete. At the end of Round 1, if further review is deemed necessary, the DIP will be scheduled for another round. Otherwise, it will be queued for the formal review and evaluation by the language authors. Thanks in advance to all who participate. Destroy!It seems reasonable. One concern I have is this: extern(delegate) ref int foo(ref int x) { return x; } int x; auto dg = &x.foo; // is dg an int * or a delegate? Currently, this means the former. I'm concerned about ambiguities and how to resolve them if all of a sudden someone decides it's a good idea to change their functions to extern(delegate), and code like this silently switches over. Or someone tries to form a delegate from one of these, but instead ends up calling the function instead. -Steve
Jul 14 2017
On Friday, 14 July 2017 at 12:52:56 UTC, Steven Schveighoffer wrote:On 7/14/17 6:43 AM, Mike Parker wrote:I thought about this use case and it's an interesting one. TLDR; it should return "int delegate()" not "int*" First I want to say this use case happens less often then you might think. It's only a problem when the function has no arguments. If the function does have arguments then omitting the parameter list clearly means the application wants a delegate/function pointer. Currently, taking the address of a member function call will return a delegate, even if it returns a reference. import std.stdio; struct Foo { static int staticInt; ref int bar() { return staticInt; } } Foo foo; writeln(typeid(&foo.bar)); // prints "int delegate()" // NOTE: If you want to take the address of the return value, you add the () to the call, i.e. writeln(typeid(&foo.bar())); // prints "int*" Furthermore, since taking the address of a UFCS call currently has no meaning, this would evaluate to int* if it was a UFCS call: ref int baz(ref Foo foo) { static int staticInt; return staticInt; } writeln(typeid(&foo.baz)); // prints "int*" So the semantic analyzer has a precendence for the '&' operator when applied to "dotted" function calls. It checks if the expression could result in a delegate, if not then it falls back to taking the address of the return value. As you have noticed, since extern(delegate) adds meaning to taking the address of a UFCS call, the semantic analyzer will now be able to create a delegate in a place where it previously couldn't. extern(delegate) ref int baz(ref Foo foo) { static int staticInt; return staticInt; } writeln(typeid(&foo.baz)); // is it "int*" or "int delegate()"? The implementation could go either way but I think it should be consistent with the semantics for delegates meaning it should result in "int delegate()" and here's why: * Having an inconsistent precedence for member functions and extern(delegate) functions would be confusing * If taking the address of the return value has precendence, we would have to come up with a new syntax when the application wants to do the opposite. Currently, if the application really means they want to take the address of the return value, they can simply add an empty parameter list to disambiguate the meaning, i.e. they could use &foo.bar() instead of &foo.bar. If we used the opposite precedence, there is currently no way to say we really want a delegate instead of taking the address of the return value. * If an existing project does start using extern(delegate), in almost all cases the change from taking the address of the return value to getting a delegate will almost always result in a compile time error. In your example if the meaning of &x.foo changed, it would return a different type which would very likely be caught by the compiler, depending of course on how the value is used. There are cases where it wouldn't be caught, but it's very unlikely. * This only affects code that goes back and modifies their application to use extern(delegate). In other words, it's not breaking existing code unless that existing code itself changes to use this feature.DIP 1011 is titled "extern(delegate)". https://github.com/dlang/DIPs/blob/master/DIPs/DIP1011.md All review-related feedback on and discussion of the DIP should occur in this thread. The review period will end at 11:59 PM ET on July 28 (3:59 AM GMT July 29), or when I make a post declaring it complete. At the end of Round 1, if further review is deemed necessary, the DIP will be scheduled for another round. Otherwise, it will be queued for the formal review and evaluation by the language authors. Thanks in advance to all who participate. Destroy!It seems reasonable. One concern I have is this: extern(delegate) ref int foo(ref int x) { return x; } int x; auto dg = &x.foo; // is dg an int * or a delegate? Currently, this means the former. I'm concerned about ambiguities and how to resolve them if all of a sudden someone decides it's a good idea to change their functions to extern(delegate), and code like this silently switches over. Or someone tries to form a delegate from one of these, but instead ends up calling the function instead. -Steve
Jul 14 2017
On 7/14/17 1:18 PM, Jonathan Marler wrote:On Friday, 14 July 2017 at 12:52:56 UTC, Steven Schveighoffer wrote:I can say from experience, this can wreak havoc on things like template constraints. Changing behavior subtly for the same syntax has consequences. I wonder if there should be a deprecation period. It's an odd case, because it only happens if the declaration is changed to extern(delegate), which means before the change would be introduced, no code would break. But I fully see quite a few people doing this because people write a lot of UFCS functions these days, and slapping an extern(delegate): label for these functions might be just the blunt instrument people reach for. I also see people doing &foo.someUFCSFunc without realizing it's not going to create a delegate. It may be obvious to the compiler that it's wrong, so they may still get an error. Where I see possible problems is things like template constraints (e.g. if(is(typeof(&foo.f) == int *)) ) However, we do have precedent that &func is different for property functions vs. normal ones, so there is some validity to your chosen path. And the opposite choice (int *) would make it impossible to take a delegate to such a function. -SteveOn 7/14/17 6:43 AM, Mike Parker wrote:I thought about this use case and it's an interesting one. TLDR; it should return "int delegate()" not "int*"DIP 1011 is titled "extern(delegate)". https://github.com/dlang/DIPs/blob/master/DIPs/DIP1011.md All review-related feedback on and discussion of the DIP should occur in this thread. The review period will end at 11:59 PM ET on July 28 (3:59 AM GMT July 29), or when I make a post declaring it complete. At the end of Round 1, if further review is deemed necessary, the DIP will be scheduled for another round. Otherwise, it will be queued for the formal review and evaluation by the language authors. Thanks in advance to all who participate. Destroy!It seems reasonable. One concern I have is this: extern(delegate) ref int foo(ref int x) { return x; } int x; auto dg = &x.foo; // is dg an int * or a delegate? Currently, this means the former. I'm concerned about ambiguities and how to resolve them if all of a sudden someone decides it's a good idea to change their functions to extern(delegate), and code like this silently switches over. Or someone tries to form a delegate from one of these, but instead ends up calling the function instead.
Jul 14 2017
On Friday, 14 July 2017 at 10:43:05 UTC, Mike Parker wrote:DIP 1011 is titled "extern(delegate)".Looks good!
Jul 14 2017
On Friday, 14 July 2017 at 10:43:05 UTC, Mike Parker wrote:DIP 1011 is titled "extern(delegate)". https://github.com/dlang/DIPs/blob/master/DIPs/DIP1011.mdContrary to other poster I believe this is wrong way to solve a problem. A far simpler approach is to allow compiler implicitly convert function pointer to delegate. The dance with making an ABI wrapper can be left to compiler proper. And no annotation hell. --- Dmitry Olshansky
Jul 14 2017
On 7/14/17 1:02 PM, Dmitry Olshansky wrote:On Friday, 14 July 2017 at 10:43:05 UTC, Mike Parker wrote:But I think the idea is to avoid the extra indirection and call, no? We already have toDelegate, which works fine if you don't care about that. -SteveDIP 1011 is titled "extern(delegate)". https://github.com/dlang/DIPs/blob/master/DIPs/DIP1011.mdContrary to other poster I believe this is wrong way to solve a problem. A far simpler approach is to allow compiler implicitly convert function pointer to delegate. The dance with making an ABI wrapper can be left to compiler proper. And no annotation hell.
Jul 14 2017
On Friday, 14 July 2017 at 17:02:37 UTC, Dmitry Olshansky wrote:...The dance with making an ABI wrapper can be left to compiler proper.This proposal doesn't make ABI wrappers, it changes the ABI of the function itself.
Jul 14 2017
On Friday, 14 July 2017 at 10:43:05 UTC, Mike Parker wrote:DIP 1011 is titled "extern(delegate)". https://github.com/dlang/DIPs/blob/master/DIPs/DIP1011.md All review-related feedback on and discussion of the DIP should occur in this thread. The review period will end at 11:59 PM ET on July 28 (3:59 AM GMT July 29), or when I make a post declaring it complete. At the end of Round 1, if further review is deemed necessary, the DIP will be scheduled for another round. Otherwise, it will be queued for the formal review and evaluation by the language authors. Thanks in advance to all who participate. Destroy!Not that it is a requirement to consider fir this DIP, but how would this play with Vittorio's lambda Value capture DIP(?)?
Jul 16 2017
On Sunday, 16 July 2017 at 11:45:09 UTC, Nicholas Wilson wrote:On Friday, 14 July 2017 at 10:43:05 UTC, Mike Parker wrote:I don't see any relation. Looks like the lambda capture lowers to defining a temporary struct and assigning the captured values to fields in that struct, then the lambda is defined as a member function of that struct. That member function already uses the delegate ABI where the context is a reference to the temporary struct. void main() { int x; string s; auto dg = [x,s]() => { // ... } /* lowers to something like this struct __lambda_struct__ { int x; string s; // This method already has a context, cannot make it extern(delegate) void execute() { // ... } } auto dg = &__lambda_struct__(x, s).execute; */ }DIP 1011 is titled "extern(delegate)". https://github.com/dlang/DIPs/blob/master/DIPs/DIP1011.md All review-related feedback on and discussion of the DIP should occur in this thread. The review period will end at 11:59 PM ET on July 28 (3:59 AM GMT July 29), or when I make a post declaring it complete. At the end of Round 1, if further review is deemed necessary, the DIP will be scheduled for another round. Otherwise, it will be queued for the formal review and evaluation by the language authors. Thanks in advance to all who participate. Destroy!Not that it is a requirement to consider fir this DIP, but how would this play with Vittorio's lambda Value capture DIP(?)?
Jul 17 2017
On Monday, 17 July 2017 at 15:16:45 UTC, Jonathan Marler wrote:On Sunday, 16 July 2017 at 11:45:09 UTC, Nicholas Wilson wrote:Fair enough.[...]I don't see any relation. Looks like the lambda capture lowers to defining a temporary struct and assigning the captured values to fields in that struct, then the lambda is defined as a member function of that struct. That member function already uses the delegate ABI where the context is a reference to the temporary struct. [...]
Jul 24 2017
On Friday, 14 July 2017 at 10:43:05 UTC, Mike Parker wrote:DIP 1011 is titled "extern(delegate)". https://github.com/dlang/DIPs/blob/master/DIPs/DIP1011.md All review-related feedback on and discussion of the DIP should occur in this thread. The review period will end at 11:59 PM ET on July 28 (3:59 AM GMT July 29), or when I make a post declaring it complete. At the end of Round 1, if further review is deemed necessary, the DIP will be scheduled for another round. Otherwise, it will be queued for the formal review and evaluation by the language authors. Thanks in advance to all who participate. Destroy!I don't like it so much but also something like this could be considered: out!(x => x>0) or out!x(x > 0) that can't collide with current syntax
Jul 24 2017
On 7/24/17 7:08 AM, Andrea Fontana wrote:On Friday, 14 July 2017 at 10:43:05 UTC, Mike Parker wrote:I think you meant this for DIP 1009? -SteveDIP 1011 is titled "extern(delegate)". https://github.com/dlang/DIPs/blob/master/DIPs/DIP1011.md All review-related feedback on and discussion of the DIP should occur in this thread. The review period will end at 11:59 PM ET on July 28 (3:59 AM GMT July 29), or when I make a post declaring it complete. At the end of Round 1, if further review is deemed necessary, the DIP will be scheduled for another round. Otherwise, it will be queued for the formal review and evaluation by the language authors. Thanks in advance to all who participate. Destroy!I don't like it so much but also something like this could be considered: out!(x => x>0) or out!x(x > 0) that can't collide with current syntax
Jul 24 2017
On Monday, 24 July 2017 at 15:27:10 UTC, Steven Schveighoffer wrote:You're right, I had too many windows opened :) AndreaI don't like it so much but also something like this could be considered: out!(x => x>0) or out!x(x > 0) that can't collide with current syntaxI think you meant this for DIP 1009? -Steve
Jul 25 2017
I know this one year old already but the DIP is still in formal review. I Think the inability to create a delegate with a custom object as its context similar to delegates created from member functions should be fixed in a library solution rather than making a language change. A simple implementation that you could try: ``` import std.traits; system ReturnType!F delegate(Parameters!F[1..$]) makeExternDelegate(F)(F fp, void* this_) { struct Delegate { void* ctxptr; F fptr; } auto dlg = Delegate(this_, fp); auto _tmp = cast(void*) &dlg; auto dlgp = cast(typeof(return)*) _tmp; return *dlgp; } unittest { struct O { int val; } auto objp = new O(1); static int getVal(O* this_) {return this_.val;} auto dlg = makeExternDelegate(&getVal, objp); dlg(); assert(dlg() == 1); objp.val = 1123; assert(dlg() != 1); assert(dlg() == 1123); } ``` However I think the compiler should still disambiguate the output of the addressof (&) operator between free and member functions, I find the difference to be rather an implementation detail that a user should not have to worry about. I also find this DIP to be like an awkward deprecation process for such disambiguation. Perhaps this should rather be on the list of breaking changes of D3. D3 would be where the language would grow up from past mistakes, even if that should break compatibility with C! I know, no body cares about that.
Jul 21 2018
On Saturday, 21 July 2018 at 17:54:17 UTC, soolaïman wrote:I know this one year old already but the DIP is still in formal review. [...]This doesn't work because the ABI of a normal function is NOT THE SAME as the ABI of a delegate. That's the only reason the DIP exists is to solve this ABI problem.
Jul 22 2018