Not only pure/nothrow/@safe should be inferred, but const on parameters and methods should be inferred too. This increases the expressiveness of alias parameters and is required for making templates const correct. Inferring const for delegate parameters reduces the annotation overhead and makes treatment of template delegate literals uniform with normal delegate literals.
IOW, the following code should compile:
int glob;
class Foo(alias a){
int x;
void foo(){a(this);}
static int bar(Foo x){return a(foo);}
}
void main(){
alias Foo!((p){glob=p.x;}) T; // parameter p inferred const
auto foo = new immutable(T);
foo.foo(); // OK, T.foo inferred const
T.bar(foo); // OK, parameter x of T.bar inferred const
(T x){glob = x.x;}(foo); // OK, parameter x inferred const
}
Comment #1 by timon.gehr — 2012-02-16T07:59:35Z
The original example contained some mistakes, second try:
int glob;
class Foo(alias a){
int x;
void foo(){a(this);}
static void bar(Foo x){a(x);}
}
void main(){
alias Foo!((a){glob=a.x;}) T; // parameter a inferred const
auto foo = new immutable(T);
foo.foo(); // OK, foo inferred const
T.bar(foo); // OK, parameter x inferred const
(T x){glob = x.x;}(foo); // ok, parameter x inferred const
}
Comment #2 by timon.gehr — 2012-02-25T07:06:34Z
This probably shouldn't be done for virtual functions of templated classes.
Comment #3 by issues.dlang — 2014-02-08T16:05:39Z
Related: issue# 8407
Comment #4 by robert.schadek — 2024-12-13T17:58:24Z