Bug 8669 – TemplateThisParameter should change member function's qualifier
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-09-16T07:49:00Z
Last change time
2013-03-11T22:06:12Z
Keywords
pull
Assigned to
nobody
Creator
k.hara.pg
Comments
Comment #0 by k.hara.pg — 2012-09-16T07:49:19Z
With current implementation, a member function with TemplateThisParameter is not generated based on each 'this' type.
struct X {
void foo(this T)() {}
}
void main() {
X mx;
mx.foo();
// T == X, and foo is mutable member function.
// then we can call it with mutable object.
immutable(X) ix;
ix.foo();
// T == immutable(X), but foo is still *mutable* member function.
// then we cannot call it with immutable object
}
foo is always instantiated as mutable member function, and always cannot be called with non-mutable object.
I think this is not convenient in many cases.
====
Enhancement:
If foo is called from immutable object, foo should be instantiated as immutable member function. Following is detailed explanation of compiler behavior to realize it.
The declaration of foo is expanded as like follows.
struct X {
template foo(this T) {
void foo() {}
}
}
If T is immutable type, compiler would wrap the template body into StorageClassDeclaration.
struct X {
template foo(this T) { // if T == immutable
immutable{ // <-- automatically wrapped by the compiler
void foo() {} // now foo is immutable member function
}
}
template foo(this T) { // if T == const
const{ // <-- automatically wrapped by the compiler
void foo() {} // now foo is const member function
}
}
// as well, if T == shared, shared const, inout, and shared inout
}
This would make forwarding operations more convenient, e.g. std.typecons.Proxy.