I have a loop traversing a graph using a 'const' reference but when I assign my iteration variable I realize that it is non-const then I get nice compiler's complains.
class ClassDescriptor
{
const string name;
const TypeInfo type;
const ClassDescriptor base;
const IPropertyDescriptor[string] propertiesByName;
IPropertyDescriptor getFlattenProperty(string name)
{
// This declaration makes 'const(ClassDescriptor) bag'
// Note that in this point I can't add ref keyword.
auto bag = this;
while(!(bag is null))
{
if(name in bag.propertiesByName)
{
return bag.propertiesByName[name];
}
// This assigment breaks the constness
bag = bag.base;
}
return null;
}
public this(string name, TypeInfo type, ClassDescriptor base, const IPropertyDescriptor[string] propertiesByName)
{
this.name = name;
this.type = type;
this.base = base;
this.propertiesByName = propertiesByName;
}
}
Sounds like a job for std.typecons.Rebindable
:
http://dpldocs.info/experimental-docs/std.typecons.Rebindable.html
import std.typecons;
Rebindable!(const typeof(this)) bag = this;
then the rest should work the same.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments