javaarrayslistinteropfrege

Converting a Frege List to a Java Array


Suppose I have a small, somewhat redundant bridge function defined in Frege

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

and some Java code that passes an already obtained TList<Long> to it

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

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

Am I passing in the wrong {context}/{instance of PrimitiveArrayElement}?


Solution

  • Primitive array types need some special treatment in Java which also inhibits full generic use in Frege.

    The reason for this is that we can't have primitive types as generics yet in 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.

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

    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. This is because those types are supported through Javas auto-boxing and auto-unboxing automatically.

    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. Because java.lang.Long is a reference type, there are no restrictions in using it as array elements.