简体   繁体   中英

Import Java library in Frege

I'm trying out frege, and I'm struggling to try to use some native Java libraries.

I'm trying it out with the leiningen plugin, and Joda time. Apparently the lein plugin doesn't take care of correctly seeting the classpath for fregec, or maybe it's related to this difference:

java -jar ~/Downloads/frege3.22.524-gcc99d7e.jar -fp ~/.m2/repository/joda-time/joda-time/2.7/joda-time-2.7.jar src/Hello.fr

Will be able to find Joda, as expected, while

java -cp ~/.m2/repository/joda-time/joda-time/2.7/joda-time-2.7.jar -jar ~/Downloads/frege3.22.524-gcc99d7e.jar src/Hello.fr 

will fail with

`org.joda.time.Years` is not a known java class

This shouldn't happen since, according to the wiki

The current class path of the running JVM plus the target directory are always on the class path.

Still, even after manually setting the -fp , this code fails to compile:

module Hello where

data JodaYears = native org.joda.time.Years where
   pure native years :: Int -> JodaYears
   pure native getYears org.joda.time.Years.getYears :: JodaYears -> Int
   --                   ^ I tried both with and without this

The error is

Instance method or getter must be applied to java reference type.

But the only instance method that I'm using (getYears), takes the reference type as input ( JodaYears )... I even tried with org.joda.time.Years , but the compilation still fails

Thanks to anyone who might shed some light on this

Brief answer, since using mobile.

You can't invoke Java with both -cp and -jar

Obviously, the class path is ignored in this case. You can try giving both jars in the -cp but then you also need to say what class to run. The frege compiler is frege.compiler.Main

Concerning the other error I think is related to "years" which is taken as instance method because of the simple name. Whereas the other method is taken as class method because of the qualified name.

The rules for defining native function foo are thus:

[ pure ] native foo XXX :: frege type

  1. For instance methods, XXX must be a simple name. You can also leave XXX out, in which case it is the same as the frege name you're defining (eg foo ).
  2. For class methods, XXX must be the fully qualified name of the method.
  3. For constructors, XXX must be "new"
  4. For member access, XXX must be ".member" where member is the actual member name.

As written by Ingo, years is believed to be an instance method, since it's missing the fully qualified name, that is: I needed to write it the other way around. The final working example of my helloworld code:

module Hello where

data JodaYears = native org.joda.time.Years where
     pure native years org.joda.time.Years.years :: Int -> JodaYears
     pure native getYears :: JodaYears -> Int

main _ = println $ JodaYears.getYears $ JodaYears.years 5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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