Bug 13845 – Error using alias of typeof(null) as unnamed lambda parameter
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
All
Creation time
2014-12-09T18:19:00Z
Last change time
2014-12-15T15:47:02Z
Assigned to
nobody
Creator
monkeyworks12
Comments
Comment #0 by monkeyworks12 — 2014-12-09T18:19:06Z
import std.variant;
import std.stdio;
alias Null = typeof(null);
alias Maybe(T) = Algebraic!(T, typeof(null));
void main()
{
Maybe!int n = 0;
writeln(n); //Prints "0"
//Prints "I'm Null!"
n.visit!(
(int) => writeln("I'm an int!"),
//Ok
(typeof(null)) => writeln("I'm Null!"),
//std/variant.d(1966): Error: static assert "__lambda2 is not a
//function or delegate" /opt/compilers/dmd2/include/std/variant.d(1766):
//instantiated from here:
//visitImpl!(true,
// VariantN!(8LU, int, typeof(null)),
// function (int _param_0)
//(Null) => writeln("I'm Null!"),
);
}
Comment #1 by peter.alexander.au — 2014-12-14T17:27:27Z
Reduced:
alias Null = typeof(null);
auto a = (int) => 0; // ok
auto b = (typeof(null)) => 0; // ok
auto c = (Null x) => 0; // ok
auto d = (Null) => 0; // error
Error: variable bug.d type void is inferred from initializer (Null) => 0, and variables cannot be of type void
Comment #2 by k.hara.pg — 2014-12-15T15:47:02Z
(In reply to Peter Alexander from comment #1)
> alias Null = typeof(null);
> auto a = (int) => 0; // ok
> auto b = (typeof(null)) => 0; // ok
`int` and `typeof(null)` are definitely parsed as types.
> auto c = (Null x) => 0; // ok
`Null x` is parsed as a parameter, typed Null and parameter name is `x`.
> auto d = (Null) => 0; // error
>
> Error: variable bug.d type void is inferred from initializer (Null) => 0,
> and variables cannot be of type void
If a lambda parameter is an identifier, it's treated as the "parameter name". Therefore it is equivalent with:
auto d = (x) => 0;
and compiler cannot infer the lambda parameter type so there's no "target" type.