Bug 809 – Should be possible to convert lazy argument to delegate

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
All
OS
All
Creation time
2007-01-07T04:44:51Z
Last change time
2019-10-24T17:16:16Z
Keywords
pull
Assigned to
Walter Bright
Creator
Li Jie

Comments

Comment #0 by cpunion — 2007-01-07T04:44:51Z
void test(lazy int dg){ int delegate() dg_ = dg; } Compile error: $ dmd -c t.d t.d(2): Error: cannot implicitly convert expression ((dg)()) of type int to int delegate()
Comment #1 by bugzilla — 2007-02-02T03:21:19Z
I don't really think it should (and it isn't documented to work). I suspect enhancing D to make this work would lead to problems down the road. I'd rather leave it as an error.
Comment #2 by smjg — 2007-03-08T17:04:13Z
How about defining &dg to extract the delegate?
Comment #3 by smjg — 2008-12-08T11:00:50Z
Reopening per no response to my last comment.
Comment #4 by razvan.nitu1305 — 2019-05-16T09:12:54Z
I don't think that dg should be viewed as a delegate even though the compiler actually creates one for the int. Logically you still have an int value which you are trying to assign to a delegate. Closing as WONTFIX.
Comment #5 by smjg — 2019-05-16T09:48:46Z
(In reply to RazvanN from comment #4) I disagree. If you have a lazy parameter, when you reference it in the function you are saying "evaluate it at this point". Essentially, a lazy parameter is just syntactic sugar for a delegate, so it makes perfect sense to be able to access said delegate. I certainly can't see any reason not to allow the programmer to do this. (I realise you could effectively achieve this with a delegate literal: int delegate() dg_ = { return dg; }; But can D compilers be relied on to optimise this to be the original delegate rather than a wrapper?) Furthermore, individuals shouldn't be marking bugs as WONTFIX just because they personally disagree with them. Let's see what Walter says.
Comment #6 by razvan.nitu1305 — 2019-05-16T11:06:37Z
(In reply to Stewart Gordon from comment #5) > (In reply to RazvanN from comment #4) > > I disagree. If you have a lazy parameter, when you reference it in the > function you are saying "evaluate it at this point". Essentially, a lazy > parameter is just syntactic sugar for a delegate, so it makes perfect sense > to be able to access said delegate. I certainly can't see any reason not to > allow the programmer to do this. > > (I realise you could effectively achieve this with a delegate literal: > int delegate() dg_ = { return dg; }; > But can D compilers be relied on to optimise this to be the original > delegate rather than a wrapper?) > You can do pragma(inline) int delegate() dg_ = { return dg; }; > Furthermore, individuals shouldn't be marking bugs as WONTFIX just because > they personally disagree with them. Let's see what Walter says. I interpreted Walter's comment and replied as an extension to his first comment.
Comment #7 by smjg — 2019-05-19T21:11:12Z
(In reply to RazvanN from comment #6) > I interpreted Walter's comment and replied as an extension to his first > comment. As I interpret Walter's comment, it's talking about Li's original idea, that one should be able to use the bare name of the lazy parameter to mean the delegate. Then I suggested defining &dg to mean the delegate. Really, the essence of this issue is that it's suggesting definining a syntax to extract the delegate from a lazy function parameter - which could be a language-defined overload of the & (address-of) operator per comment 2, or it could be something else entirely.
Comment #8 by andrei — 2019-06-04T15:27:13Z
On the face of it, a lazy parameter is a delegate so the request makes sense. On the other hand lazy T is meant to be "pretty much the same as T". This works today: void test(lazy int dg){ int delegate() dg_ = { return dg; }; } I'm not sure how it interacts with attributes and qualifiers. This could be made to work: void test(lazy int dg){ int delegate() dg_ = &dg; } Currently, oddly enough (and definitely a bug), this issues an unfit error message: void test(lazy int dg){ auto x = &dg; } "Error: lazy variable dg cannot be modified" :o) So it seems like taking the address of a lazy is "available" and could return the underlying delegate.
Comment #9 by dlang-bot — 2019-08-02T11:30:40Z
@RazvanN7 created dlang/dmd pull request #10264 "Fix Issue 809 - Should be possible to convert lazy argument to delegate" fixing this issue: - Fix Issue 809 - Should be possible to convert lazy argument to delegate https://github.com/dlang/dmd/pull/10264
Comment #10 by dlang-bot — 2019-08-02T12:19:06Z
@RazvanN7 created dlang/dlang.org pull request #2686 "Spec change for Issue 809" mentioning this issue: - Spec change for Issue 809 Merge after dmd PR: https://github.com/dlang/dmd/pull/10264 https://github.com/dlang/dlang.org/pull/2686
Comment #11 by dlang-bot — 2019-08-26T08:57:42Z
dlang/dmd pull request #10264 "Fix Issue 809 - Should be possible to convert lazy argument to delegate" was merged into master: - d105c1c648f7944e84422276d19c21a39917afb3 by RazvanN7: Fix Issue 809 - Should be possible to convert lazy argument to delegate https://github.com/dlang/dmd/pull/10264
Comment #12 by dlang-bot — 2019-10-24T17:16:16Z
dlang/dlang.org pull request #2686 "Spec change for Issue 809" was merged into master: - c9fdeeca8e2c26a180c9db2b136864ba4ee9e576 by Razvan Nitu: Spec change for Issue 809 Merge after dmd PR: https://github.com/dlang/dmd/pull/10264 https://github.com/dlang/dlang.org/pull/2686