title: Associative Array where a class TypeInfo is key in D?
body: <p>I'd like to be able to create an multidim associative array where one dimension is a class. Like this:</p>
  
  <pre><code>class Node{
      Node[?classType?][string] inputs;
  }
  </code></pre>
  
  <p>so that I later can do</p>
  
  <pre><code>Node[] getInputsOfType(?? aClass){
     if(aClass in this.inputs)
        return this.inputs[aClass];
     else
        return null;
  }
  
  // meanwhile in another file...
  
  Node[] effects = someAudioNode.getInputsOfType(AudioEffect);
  </code></pre>
  
  <p>I'm just lost. Any ideas?<br>
  Regarding the last part: Can a class be used as a parameter all by itself like that? (<code>AudioEffect</code> in this example is a class.)</p>
  
  <p>BR</p>
  
  <p><strong>[update/resolution]</strong></p>
  
  <p>Thanks for the answer(s)! </p>
  
  <p>I thought it'd be nice to post the result. Ok, I looked up <code>.classinfo</code> in the source code and found that it returns an instance of TypeInfo_Class, and that there's a <code>.name</code>-property, a <code>string</code>. So this is what I came up with: </p>
  
  <pre><code>#!/usr/bin/env dmd -run
  import  std.stdio;
  class A{
      int id;
      static int newId;
      A[string][string] list;
      this(){ id = newId++; }
      void add(A a, string name){
          writefln("Adding: [%s][%s]", a.classinfo.name, name);
          list[a.classinfo.name][name] = a;
      }
      T[string] getAllOf(T)(){
          return cast(T[string]) list[T.classinfo.name];
      }
  }
  class B : A{ }
  void main(){
      auto a = new A();
      a.add(new A(), "test");
      a.add(new B(), "bclass");
      a.add(new B(), "bclass2");
  
      auto myAList = a.getAllOf!(A);
      foreach(key, item; myAList)
          writefln("k: %s, i: %s id: %s",
                  key, item.toString(), item.id);
  
      auto myBList = a.getAllOf!(B);
      foreach(key, item; myBList)
          writefln("k: %s, i: %s id: %s",
                  key, item.toString(), item.id);
  }
  </code></pre>
  
  <p>It outputs:</p>
  
  <pre><code>Adding: [classtype.A][test]
  Adding: [classtype.B][bclass]
  Adding: [classtype.B][bclass2]
  Trying to get [classtype.A]
  k: test, i: classtype.A id: 1
  Trying to get [classtype.B]
  k: bclass2, i: classtype.B id: 3
  k: bclass, i: classtype.B id: 2
  </code></pre>
  
  <p>So yeah, I suppose it works. Yey! Anyone has ideas for improvement?</p>
  
  <p>Are there any pitfalls here? </p>
  
  <ul>
  <li>Can <code>classinfo.name</code> suddenly behave unexpedictly?</li>
  <li>Is there a »proper» way of getting the class name?</li>
  </ul>
  
  <p>Also, is this the fastest way of doing it? I mean, all class names seems to start with <code>classtype.</code>. Oh well, that's perhaps another SO-thread. Thanks again!</p>
  
  <p>BR</p>