简体   繁体   English

将Frege列表转换为Java数组

[英]Converting a Frege List to a Java Array

Suppose I have a small, somewhat redundant bridge function defined in Frege 假设我在Frege中定义了一个小的,有点冗余的桥接函数

listToArray :: (PrimitiveArrayElement α) => [α] -> JArray α
listToArray = arrayFromList

and some Java code that passes an already obtained TList<Long> to it 以及一些将已经获得的TList<Long>传递给它的Java代码

TList<Long> tl_results = ...
Long[] results = FregeStuffies.listToArray(IPrimitiveArrayElement_Long.it, Thunk.lazy(results));

Eclipse complains that the arguments passed to FregeStuffies.listToArray are not applicable to what it is Eclipse抱怨传递给FregeStuffies.listToArray的参数不适用于它的内容

listToArray(PreludeArrays.CPrimitiveArrayElement<α[],α>, Lazy<PreludeBase.TList<α>>)

Am I passing in the wrong {context}/{instance of PrimitiveArrayElement }? 我是否传递了错误的{context} / { PrimitiveArrayElement实例}?

Primitive array types need some special treatment in Java which also inhibits full generic use in Frege. 原始数组类型需要在Java中进行一些特殊处理,这也禁止在Frege中完全使用泛型。

The reason for this is that we can't have primitive types as generics yet in Java. 这样做的原因是我们在Java中不能将原始类型作为泛型。 When we have 当我们有

 static<A> ... foo(A[] arg) { ... }

in Java, we cannot instantiate the type variable A with a primitive type like int or long . 在Java中,我们无法使用intlong等基本类型实例化类型变量A

What this means is that, as it stands, your listToArray function is not going to work for Frege types that are based on Java primitive types (it is said that such things will be supported in Java 10). 这意味着,就目前而言, listToArray函数不适用于基于Java原始类型的Frege类型(据说Java 10中将支持这些内容)。

For arrays of primitive types, you need to know which type it is and then call the appropriate method, in your case: 对于基本类型的数组,您需要知道它是哪种类型,然后在您的情况下调用适当的方法:

PreludeArrays.IPrimitiveArrayElement_Long.arrayFromList

Note also that boxed primitive types are currently not explicitly supported by Frege. 另请注意,Frege目前尚未明确支持盒装基元类型。 This is because those types are supported through Javas auto-boxing and auto-unboxing automatically. 这是因为这些类型通过Javas自动装箱和自动取消装箱自动支持。

If you really need an array of boxed long , you can define java.lang.Long as a native type, make it an instance of ArrayElement and provide explicit boxing and unboxing functions. 如果你真的需要一个盒装long的数组,你可以将java.lang.Long定义为本机类型,使其成为ArrayElement的实例并提供明确的装箱和拆箱功能。 Because java.lang.Long is a reference type, there are no restrictions in using it as array elements. 因为java.lang.Long是一个引用类型,所以将它用作数组元素没有任何限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM