Search code examples
javaarraysfrege

Mapping Java Array to Frege


Lets say I'd like to map the Java code:

package mypackage;

class A {
    public String[] values() {
       return new String[]{"one", "two"};
    }
}

To its Frege counterpart:

data AA = pure native mypackage.A where
    native values :: AA -> [String]

At the moment Frege complains with:

error: incompatible types: String[] cannot be converted to TList

How can I map a Java array to Frege ?


Solution

  • The message is actually from the Java compiler.

    The Frege type that corresponds to a Java array

    Foo[]
    

    is

    JArray Bar
    

    where Bar is the Frege type that corresponds to Foo.

    So, in your case it should be

    JArray String
    

    Note that this is an immutable array from the Frege point of view. If you want a mutable array, use

    Mutable s (JArray String)
    

    but, of course, it can only be used in the ST monad.

    Here is the link to the relevant online doc: http://www.frege-lang.org/doc/frege/prelude/PreludeArrays.html Because this is part of Prelude, you don't need to import anything to use it.