Vala 中作为 return 值的可空指针
Nullable pointer as return value in Vala
假设我们有这样的东西(这只是一个例子)
public interface Foo : GLib.Object {
public abstract double *f();
}
public class Toto : GLib.Object, Foo {
private double i;
public Toto(double i = 0) {
this.i = i;
}
public double *f() {
return &i;
}
public static int main(string[] args) {
Foo a = new Toto(42.0);
double i = *a.f();
stdout.printf("%.3f\n", i);
return 0;
}
}
此代码工作得很好,但问题是 Foo
必须与 public abstract T *f()
通用,因此 Toto
必须实现 Foo<double>
,但
`double' is not a supported generic type argument
(我的第一个问题是 "why?",据我所知我可以毫无问题地使用 int 为例)
所以它是 Foo<double?>
,我需要类似 double i = (!) *(a.f())
的东西,但它不起作用(在 C 级)
error: invalid use of void expression i = (gdouble) (*(*_tmp1_));
那么我如何使用 f()
方法?
(我的vala版本是0.36.3)
你为什么首先在 Vala 中使用指针? (这是不鼓励的,指针是针对极端情况的语言。)
Vala 中的可空类型是生成的 C 代码中的指针。
所以解决这个问题的一种方法是:
public interface Foo<T> : GLib.Object {
public abstract T f();
}
public class Toto : GLib.Object, Foo<double?> {
private double i;
public Toto(double i = 0) {
this.i = i;
}
public double? f() {
return i;
}
public static int main(string[] args) {
Foo<double?> a = new Toto(42.0);
double? i = a.f();
stdout.printf("%.3f\n", (!) i);
return 0;
}
}
这里编译并与 valac 0.36.3 完美配合。
为 Toto.f
生成的 C 函数的类型签名是:
static gdouble* toto_real_f (Foo* base);
假设我们有这样的东西(这只是一个例子)
public interface Foo : GLib.Object {
public abstract double *f();
}
public class Toto : GLib.Object, Foo {
private double i;
public Toto(double i = 0) {
this.i = i;
}
public double *f() {
return &i;
}
public static int main(string[] args) {
Foo a = new Toto(42.0);
double i = *a.f();
stdout.printf("%.3f\n", i);
return 0;
}
}
此代码工作得很好,但问题是 Foo
必须与 public abstract T *f()
通用,因此 Toto
必须实现 Foo<double>
,但
`double' is not a supported generic type argument
(我的第一个问题是 "why?",据我所知我可以毫无问题地使用 int 为例)
所以它是 Foo<double?>
,我需要类似 double i = (!) *(a.f())
的东西,但它不起作用(在 C 级)
error: invalid use of void expression
i = (gdouble) (*(*_tmp1_));
那么我如何使用 f()
方法?
(我的vala版本是0.36.3)
你为什么首先在 Vala 中使用指针? (这是不鼓励的,指针是针对极端情况的语言。)
Vala 中的可空类型是生成的 C 代码中的指针。
所以解决这个问题的一种方法是:
public interface Foo<T> : GLib.Object {
public abstract T f();
}
public class Toto : GLib.Object, Foo<double?> {
private double i;
public Toto(double i = 0) {
this.i = i;
}
public double? f() {
return i;
}
public static int main(string[] args) {
Foo<double?> a = new Toto(42.0);
double? i = a.f();
stdout.printf("%.3f\n", (!) i);
return 0;
}
}
这里编译并与 valac 0.36.3 完美配合。
为 Toto.f
生成的 C 函数的类型签名是:
static gdouble* toto_real_f (Foo* base);