2012-12-31 31 views
3

我有以下的小例子(VALA 0.18.1):從什麼小的文件我已經能夠瓦拉接口泛型編譯器錯誤

/src/Test.vala.c: In function ‘learning_test_main’: 
/src/Test.vala.c:253:2: error: incompatible type for argument 2 of ‘learning_iexample_set_to’ 
/src/Test.vala.c:117:6: note: expected ‘gconstpointer’ but argument is of type ‘double’ 
error: cc exited with status 256 
Compilation failed: 1 error(s), 0 warning(s) 

現在:

namespace Learning 
{ 
    public interface IExample<T> 
    { 
     public abstract void set_to(T val); 
     public abstract T get_to(); 
    } 

    public class Example : Object, IExample<double> 
    { 
     private double to; 

     public void set_to(double val) 
     { 
     to = val; 
     } 

     public double get_to() 
     { 
     return to; 
     } 

     public string to_string() 
     { 
     return "Example: %.5f".printf(to); 
     } 
    } 

    public class Test 
    { 
     public static void main(string[] args) 
     { 
     stdout.printf("Start test\n"); 

     Example ex = new Example(); 

     stdout.printf("%s\n", ex.to_string()); 
     ex.set_to(5.0); 
     stdout.printf("%s\n", ex.to_string()); 

     stdout.printf("End test\n"); 
     } 
    } 
} 

這引發錯誤找到Vala中的泛型接口,http://www.vala-project.org/doc/vala-draft/generics.html#genericsexamples,這應該工作。當我檢查生成的C代碼時,它將Example的set_to函數顯示爲一個double,將IExample的set_to函數顯示爲一個gconstpointer。

那麼爲什麼主要功能,然後使用gconstpointer版本,而不是雙版本?有人可以向我解釋爲什麼它不起作用和解決它的方法嗎?

謝謝你的幫助。

P.S.是的,我知道找到的文檔是一個草稿文檔。

答案代碼: 根據以下選定的答案,這是我改變了代碼。

namespace Learning 
{ 
    public interface IExample<T> 
    { 
     public abstract void set_to(T val); 
     public abstract T get_to(); 
    } 

    public class Example : Object, IExample<double?> 
    { 
     private double? to; 

     public void set_to(double? val) 
     { 
     to = val; 
     } 

     public double? get_to() 
     { 
     return to; 
     } 

     public string to_string() 
     { 
     return (to == null) ? "NULL" : "Example: %.5f".printf(to); 
     } 
    } 

    public class Test 
    { 
     public static void main(string[] args) 
     { 
     stdout.printf("Start test\n"); 

     Example ex = new Example(); 

     stdout.printf("%s\n", ex.to_string()); 
     ex.set_to(5.0); 
     stdout.printf("%s\n", ex.to_string()); 

     stdout.printf("End test\n"); 
     } 
    } 
} 

回答

1

而不是使用IExample<double>,使用IExample<double?>框的double,以便它可以作爲一個指針進行傳遞。對於Vala中的任何struct類型,這通常是必需的。類和緊湊類不需要這種處理,因爲它們已經是指針了。此外,可以在不裝箱的情況下直接使用小於32位(即使在64位平臺上)的struct類型(如uint8char)。如有疑問,請填寫。

+0

謝謝。我拿出了你的答案,並用上面的代碼發佈了該版本。非常感謝你。 –