Issue 9423 - Missed conversion of lambda literal with ref argument
Summary: Missed conversion of lambda literal with ref argument
Status: RESOLVED DUPLICATE of issue 11316
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords: pull, rejects-valid
: 12830 (view as issue list)
Depends on:
Blocks:
 
Reported: 2013-01-29 02:51 UTC by bearophile_hugs
Modified: 2023-01-07 22:25 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2013-01-29 02:51:30 UTC
void foo(int delegate(ref int[1]) spam) {}
void main() {
    foo((ref int[1] x) => 0); // OK
    foo(x => 0); // Error
}



DMD 2.062alpha gives:

test.d(4): Error: function test.foo (int delegate(ref int[1u]) spam)(null) is not callable using argument types (void)
test.d(4): Error: cannot implicitly convert expression (__lambda3) of type int delegate(int[1u] x) pure nothrow @safe to int delegate(ref int[1u])
Comment 1 Kenji Hara 2013-01-29 09:23:54 UTC
When I fixed bug7705, I decided that lambda inference does not infer parameter storage classes. Instead, users should specify `ref`/`out`/`lazy` explicitly.

https://github.com/D-Programming-Language/dmd/pull/809/files#L1R496

Because, explicit specifying of `ref` in call site is sometimes required in newsgroup.

void foo(ref int x) {}
int n;
foo(ref n);   // not allowed in today

So, current behavior is intended. You should write it as follows:

void foo(int delegate(ref int[1]) spam) {}
void main() {
    foo((ref x) => 0); // OK
}

However, I cannot say clearly whether it's right behavior.
I think that the opinion "parameter storage classes should be inferred" is also worth.

Therefore, I'll mark this as 'enhancement'.

=====

A pull to implement this feature:
https://github.com/D-Programming-Language/dmd/pull/1580
Comment 2 bearophile_hugs 2013-01-29 10:00:15 UTC
(In reply to comment #1)

> Therefore, I'll mark this as 'enhancement'.
> 
> =====
> 
> A pull to implement this feature:
> https://github.com/D-Programming-Language/dmd/pull/1580

Thank you Hara.
Comment 3 bearophile_hugs 2013-01-30 10:26:15 UTC
A discussion thread, the answers are mixed:

http://forum.dlang.org/thread/mixmakdqfmaznmmnizux@forum.dlang.org


A comment from Timon Gehr:

> BTW, the pull does not contain a test for the case
> 
> void foo(int delegate(int) dg){ ... }     // 1
> void foo(int delegate(ref int) dg){ ... } // 2
> 
> void main(){ foo(x=>0); } // call 1
Comment 4 Mathias LANG 2020-08-26 17:27:59 UTC
*** Issue 12830 has been marked as a duplicate of this issue. ***
Comment 5 Paul Backus 2023-01-07 22:25:06 UTC
Closing in favor of 11316, since that one covers all storage classes, not just ref.

*** This issue has been marked as a duplicate of issue 11316 ***