我有以下的小例子(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");
}
}
}
謝謝。我拿出了你的答案,並用上面的代碼發佈了該版本。非常感謝你。 –