Bug 7198 – Delegate literals with nameless arguments fail to infer a type
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-01-02T05:07:52Z
Last change time
2021-10-25T05:10:13Z
Keywords
rejects-valid
Assigned to
No Owner
Creator
Rainer Schuetze
Comments
Comment #0 by r.sagitario — 2012-01-02T05:07:52Z
Maybe this is an expected side-effect of more magic with delegate type inference, but the latest dmd version from github causes errors with this code:
module test;
class Widget {}
void main()
{
auto dg0 = delegate void(Widget w) { }; // OK
auto dg1 = delegate void(Widget) { }; // error
void delegate(Widget) dg2 = delegate void(Widget) { }; //OK
void delegate(Widget) dg3;
dg3 = delegate void(Widget) { foo(); }; //error
}
test.d(8): Error: cannot infer type from ambiguous function literal __dgliteral2
test.d(8): Error: __dgliteral2 has no value
test.d(11): Error: __dgliteral6 has no value
This compiles with dmd 2.057.
I guess the trouble is that the delegate argument "Widget" is interpreted as a parameter name, not the type. Using "int" instead of "Widget" compiles.
Comment #1 by k.hara.pg — 2012-01-02T05:35:59Z
> I guess the trouble is that the delegate argument "Widget" is interpreted as a
parameter name, not the type. Using "int" instead of "Widget" compiles.
Yes, you're right.
And that is inevitable side-effect of parameter type inference.
Walter answered about the decision in https://github.com/D-Programming-Language/dmd/pull/588 .
So this issue should be marked as 'resolved-invalid' or 'resolved-wontfix', IMO.
Comment #2 by alex — 2012-01-02T05:40:07Z
Why not just disallow unnamed parameters entirely? Seems like cleaner language design to me. What we have now is clearly ambiguous and cannot be resolved, so...
Comment #3 by alex — 2012-01-02T05:46:26Z
(I don't actually know why we have unnamed parameters at all; most modern languages simply don't allow this. In addition, unused parameters in delegate/function literals/lambdas sort of seems to go against the entire idea with lambda functions, in the general case.)
Comment #4 by doob — 2012-01-02T06:35:27Z
(In reply to comment #3)
> (I don't actually know why we have unnamed parameters at all; most modern
> languages simply don't allow this. In addition, unused parameters in
> delegate/function literals/lambdas sort of seems to go against the entire idea
> with lambda functions, in the general case.)
Useful situations for unnamed parameters:
* Declaring a delegate type
void delegate (int) dg;
* Declaring a function/method without implementation
void foo (int);
* Overriding/implementing a method where a parameter isn't needed
class Foo {
abstract void foo (int a);
}
class Bar : Foo {
void foo (int) {}
}
These are the situations I see it as might being useful but I would say that adding names to the parameters adds documentation and that's always a good thing.
Comment #5 by alex — 2012-01-02T06:41:35Z
(In reply to comment #4)
> (In reply to comment #3)
> > (I don't actually know why we have unnamed parameters at all; most modern
> > languages simply don't allow this. In addition, unused parameters in
> > delegate/function literals/lambdas sort of seems to go against the entire idea
> > with lambda functions, in the general case.)
>
> Useful situations for unnamed parameters:
>
> * Declaring a delegate type
>
> void delegate (int) dg;
But that's a type signature, not a literal.
>
> * Declaring a function/method without implementation
>
> void foo (int);
This, on the other hand, I do not like. Without a parameter name, you have to look at the implementation to have a clue what it means. That makes the declaration (more or less) useless.
>
> * Overriding/implementing a method where a parameter isn't needed
>
> class Foo {
> abstract void foo (int a);
> }
>
> class Bar : Foo {
> void foo (int) {}
> }
>
Point taken, though naming it _ or similar usually works.
>
> These are the situations I see it as might being useful but I would say that
> adding names to the parameters adds documentation and that's always a good
> thing.
Agreed.
Comment #6 by r.sagitario — 2012-01-02T07:44:36Z
Ok, I understand.
There are already a number of situation where the decision Type/Variable is deferred to the semantic phase. Would it be possible to do the same here?
If not, I think the same syntax for delegate literals should be forbidden for built-in types for consistency.
Comment #7 by doob — 2012-01-02T09:09:00Z
(In reply to comment #5)
> (In reply to comment #4)
> > (In reply to comment #3)
> > > (I don't actually know why we have unnamed parameters at all; most modern
> > > languages simply don't allow this. In addition, unused parameters in
> > > delegate/function literals/lambdas sort of seems to go against the entire idea
> > > with lambda functions, in the general case.)
> >
> > Useful situations for unnamed parameters:
> >
> > * Declaring a delegate type
> >
> > void delegate (int) dg;
>
> But that's a type signature, not a literal.
Oh, yeah, right.
> >
> > * Declaring a function/method without implementation
> >
> > void foo (int);
>
> This, on the other hand, I do not like. Without a parameter name, you have to
> look at the implementation to have a clue what it means. That makes the
> declaration (more or less) useless.
I've seen it a lot when declaring C functions. There won't be an implementation (at least not in your code) and you're relying on the documentation for the C library.
> >
> > * Overriding/implementing a method where a parameter isn't needed
> >
> > class Foo {
> > abstract void foo (int a);
> > }
> >
> > class Bar : Foo {
> > void foo (int) {}
> > }
> >
>
> Point taken, though naming it _ or similar usually works.
That will only work for one argument.
> >
> > These are the situations I see it as might being useful but I would say that
> > adding names to the parameters adds documentation and that's always a good
> > thing.
>
> Agreed.
Note that I'm not against this idea. Just pointing out how/when it can be used.
Comment #8 by k.hara.pg — 2012-01-02T16:59:46Z
(In reply to comment #6)
> Ok, I understand.
>
> There are already a number of situation where the decision Type/Variable is
> deferred to the semantic phase. Would it be possible to do the same here?
>
> If not, I think the same syntax for delegate literals should be forbidden for
> built-in types for consistency.
It's a small, but good improvement for consistent. I'll post a patch to fix it.
Comment #9 by yebblies — 2012-01-31T18:20:47Z
Not a regression, but the result of a language change that happens to break some existing code.
Comment #10 by k.hara.pg — 2013-08-06T17:21:36Z
*** Issue 10767 has been marked as a duplicate of this issue. ***
Comment #11 by andrej.mitrovich — 2013-08-06T17:44:35Z
(In reply to comment #1)
> > I guess the trouble is that the delegate argument "Widget" is interpreted as a
> parameter name, not the type. Using "int" instead of "Widget" compiles.
>
> Yes, you're right.
> And that is inevitable side-effect of parameter type inference.
>
> Walter answered about the decision in
> https://github.com/D-Programming-Language/dmd/pull/588 .
>
> So this issue should be marked as 'resolved-invalid' or 'resolved-wontfix',
> IMO.
This is a bummer for code that deals with signals, e.g.:
signal.connect( (Widget widget, Event) {
// ignores Event argument, but does something useful with a widget
});
If 'connect' is a function typed like so:
void connect(void function(Widget, Event)) { }
Then all works fine. However signals can typically take functions which do or don't return a value (functions with different return types), and signals can typically take both functions and delegates. So the connect method has to become a templated function which uses some traits and wraps this in a constraint, e.g.:
void connect(T)(T t) /* if (Constraint!T) */ { }
Even without the constraint this immediately fails at the call site due to this current Issue 7198.
I guess the only workaround is to use mixins to generate a number of connect methods, so they become:
void connect(void function(Widget, Event)) { }
void connect(void delegate(Widget, Event)) { }
void connect(bool function(Widget, Event)) { }
void connect(bool delegate(Widget, Event)) { }
And then type inference will work properly. It's far from ideal though, as you have to hardcode these combinations rather than allow arbitrary functions as signal handlers (e.g. functions with default parameters).
But what sucks the most is the standard template instantiation error, where the compiler tells you nothing about what went wrong.
But otherwise, I don't see a solution for this issue.
Comment #12 by yebblies — 2013-11-26T23:04:25Z
This was intentional, as stated in other comments.
Comment #13 by pro.mathias.lang — 2021-10-25T05:10:13Z
Just hit this as I was about to report it as a bug.
I came from this angle: https://github.com/dlang-community/D-Scanner/issues/846
So it was surprising, to say the least. And I don't think the grammar reflects this correctly.