Home > other >  Why missing generic type arguments here?
Why missing generic type arguments here?

Time:09-27

I am trying following simple code where a HashTable is created in main and sent to another function. There a list of all keys is to be created and printed:

public static void hashtable2list(HashTable sht){
    List lst = new List<string>(); 
    sht.foreach( (key, value) => { lst.append(key); } ); 
    lst.foreach((entry) => {print("%s\n", (string)entry);});  
    return; 
}

public static void main(){
    HashTable <string, int> ht = new HashTable <string, int> (direct_hash, direct_equal);
    ht.insert("one", 1); 
    ht.insert("two", 2); 
    ht.insert("three", 3); 
    hashtable2list(ht); 
    return; 
}

However, when I try to compile above, I get following error which occurs where items are to be appended to the list:

$ valac mySegFaultAnother.vala
rnSegFaultAnother.vala:5.33-5.35: error: missing generic type arguments
    sht.foreach( (key, value) => { lst.append(key); } ); 
                                   ^^^
Compilation failed: 1 error(s), 0 warning(s)

Where is the problem and how can this be solved?

CodePudding user response:

Because you've created HashTable<string, int> , but hashtable2list accepts only HashTable - ie it doesn't know which specialization types HashTable has therefore it can't correctly compile.

  • Related