如何通过 D 中的 const 引用循环遍历图形?
How can I loop traverse a graph through const reference in D?
我有一个使用 'const' 引用遍历图形的循环,但是当我分配我的迭代变量时,我意识到它是非常量然后我得到编译器的抱怨。
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;
}
}
听起来像是 std.typecons.Rebindable
的工作:
http://dpldocs.info/experimental-docs/std.typecons.Rebindable.html
import std.typecons;
Rebindable!(const typeof(this)) bag = this;
那么其余的应该是一样的。
我有一个使用 'const' 引用遍历图形的循环,但是当我分配我的迭代变量时,我意识到它是非常量然后我得到编译器的抱怨。
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;
}
}
听起来像是 std.typecons.Rebindable
的工作:
http://dpldocs.info/experimental-docs/std.typecons.Rebindable.html
import std.typecons;
Rebindable!(const typeof(this)) bag = this;
那么其余的应该是一样的。