Vala接口泛型编译错误 [英] Vala interface generics compiler error

查看:174
本文介绍了Vala接口泛型编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下小例子(vala 0.18.1):

 命名空间学习
{
public interface I< T>
{
public abstract void set_to(T val);
public abstract T get_to();
}

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

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

public double get_to()
{
return to;
}

public string to_string()
{
returnExample:%.5f.printf(to);
}
}

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

示例ex = new Example();

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

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

这会抛出错误: / p>

  /src/Test.vala.c:在函数'learning_test_main'中:
/src/Test.vala.c :253:2:错误:learning_iexample_set_to的参数2的不兼容类型
/src/Test.vala.c:117:6:note:预期的gconstpointer,但参数类型为double
错误:cc退出状态为256
编译失败:1错误,0警告

现在,我在Vala中的泛型接口上找到了一些文档, http://www.vala-project.org/doc/vala-draft/generics.html#genericsexamples ,这应该可以工作。当我检查生成的C代码,它显示示例的set_to函数作为一个double和IExample的set_to函数作为一个gconstpointer。



那么为什么main函数使用gconstpointer版本而不是双版本?



感谢您的帮助。



PS是的,我知道找到的文档是一个文档草稿。



答案代码:
根据下面的选择答案这是我把代码更改为



 命名空间学习
{
public interface IExample< T>
{
public abstract void set_to(T val);
public abstract T get_to();
}

public class示例:Object,IExample< double?>
{
private double?至;

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

public double? get_to()
{
return to;
}

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

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

示例ex = new Example();

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

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


解决方案>

使用 IExample< double?> 而不是使用 IExample< double> c $ c> double ,以便它可以作为指针传递。这对于在Vala中的任何 struct 类型都是必要的。类和紧凑类不需要这种处理,因为它们已经是指针。此外, struct 类型小于32位(即使在64位平台上),例如 uint8 char 可以直接使用而无需装箱。当有疑问,盒。


I have the following small example(vala 0.18.1):

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");
      }
   }
}

This throws the error:

/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)

Now from what little documentation I have been able to find on generics interfaces in Vala, http://www.vala-project.org/doc/vala-draft/generics.html#genericsexamples, this should work. When I check the resulting C code, it shows the Example's set_to function as taking a double and the IExample's set_to function as taking a gconstpointer.

So why is the main function then using the gconstpointer version instead of the double version? Can some one explain to me why it does not work and a way to get around it?

Thank you for your help.

P.S. Yes I know the documentation found is a draft document.

ANSWER CODE: According to the selected answer below this is what I changed the code to.

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");
      }
   }
}

解决方案

Rather than using IExample<double>, use IExample<double?> to box the double so that it can be passed as a pointer. This necessary, generally, for any struct type in Vala. Classes and compact classes do not need this treatment, as they are already pointers. Also, struct types that are smaller than 32-bits (even on 64-bit platforms) such as uint8 or char can be used directly without boxing. When in doubt, box.

这篇关于Vala接口泛型编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆