2016-05-06 17 views
2

Ich habe eine Schleife durchläuft ein Diagramm mit einem 'const' Verweis, aber wenn ich meine Iteration Variable zuweisen, merke ich, dass es nicht-const ist, dann bekomme ich nette Compiler-Beschwerden.Wie kann ich einen Graphen durch einen konstanten Bezug in D durchlaufen?

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; 
    } 
} 
+0

Profi-Tipp: Sie können 'while (Tasche! Ist Null)' schreiben. Gleiches mit 'in'. – sigod

+0

danke! :) Ich wusste nicht –

+0

Ich bin kein D-Programmierer, aber warum können Sie Zeiger nicht verwenden? – Elazar

Antwort