Bug 3235 – [tdpl] Function literals must be deduced as "function" or "delegate"
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2009-08-08T07:16:00Z
Last change time
2011-12-31T12:22:38Z
Keywords
patch
Assigned to
nobody
Creator
andrei
Comments
Comment #0 by andrei — 2009-08-08T07:16:05Z
Consider:
void foo(alias pred)() {
pragma(msg, pred.stringof);
}
unittest {
foo!((i) { return i < 0; })();
int z = 1;
foo!((i) { return i < z; })();
}
void main(string[] args)
{
}
This outputs:
__funcliteral1(__T2)
__dgliteral3(__T4)
because the first literal does not have to be a delegate.
Comment #1 by andrei — 2009-08-08T07:17:17Z
(In reply to comment #0)
> Consider:
>
> void foo(alias pred)() {
> pragma(msg, pred.stringof);
> }
>
> unittest {
> foo!((i) { return i < 0; })();
> int z = 1;
> foo!((i) { return i < z; })();
> }
>
> void main(string[] args)
> {
> }
>
> This outputs:
>
> __funcliteral1(__T2)
> __dgliteral3(__T4)
>
> because the first literal does not have to be a delegate.
I meant that right now this outputs:
__dgliteral1(__T2)
__dgliteral3(__T4)
and it should output:
__funcliteral1(__T2)
__dgliteral3(__T4)
Comment #2 by smjg — 2009-08-08T11:14:14Z
This is as designed.
http://www.digitalmars.com/d/1.0/expression.html#FunctionLiteral
"If the keywords function or delegate are omitted, it defaults to being a delegate."
Having the type of a function/delegate literal depend on its contents like this makes it hard for someone reading the code to tell which it is. Since function pointers and delegates are not generally interchangeable, AISI it's desirable to keep their literal notations distinct.
Comment #3 by andrei — 2009-08-08T11:55:19Z
(In reply to comment #2)
> This is as designed.
>
> http://www.digitalmars.com/d/1.0/expression.html#FunctionLiteral
> "If the keywords function or delegate are omitted, it defaults to being a
> delegate."
>
> Having the type of a function/delegate literal depend on its contents like this
> makes it hard for someone reading the code to tell which it is. Since function
> pointers and delegates are not generally interchangeable, AISI it's desirable
> to keep their literal notations distinct.
I think type deduction should help here as much as anywhere else. If you do want a delegate, you can always use the delegate keyword. Plus, all algorithms in std.algorithm take an efficiency toll when used with literals.
Comment #4 by andrei — 2009-08-08T11:57:05Z
(In reply to comment #2)
> This is as designed.
>
> http://www.digitalmars.com/d/1.0/expression.html#FunctionLiteral
> "If the keywords function or delegate are omitted, it defaults to being a
> delegate."
>
> Having the type of a function/delegate literal depend on its contents like this
> makes it hard for someone reading the code to tell which it is. Since function
> pointers and delegates are not generally interchangeable, AISI it's desirable
> to keep their literal notations distinct.
I think type deduction should help here as much as anywhere else. If you do want a delegate, you can always use the delegate keyword. Plus, all algorithms in std.algorithm take an efficiency toll when used with literals.
(Please do not mark this as resolved; I've discussed with Walter before posting it, and he suggested I do. In general, the spec of D2 is fluid enough to not be a strong reason for invalidating a bug report. Thanks.)
Comment #5 by andrei — 2011-04-28T13:39:40Z
BTW this is in TDPL now, with Walter's approval.
Comment #6 by andrei — 2011-12-18T20:05:41Z
The following example is in TDPL:
unittest
{
auto f = (int i) {};
assert(is(f == function));
}
The example fails to compile (it should). Also the following example should work:
unittest
{
int a;
auto f = (int i) { a = i; };
assert(is(f == delegate));
}
The compiler should automatically infer function or delegate type depending on the literal's use of local state. The argument against it - people change a detail in the body of the function and its type changes - is non sequitur. Changing a literal from 10 to 10.0 also changes its type. Please fix.