简体   繁体   English

在Frege中导入Java库

[英]Import Java library in Frege

I'm trying out frege, and I'm struggling to try to use some native Java libraries. 我正在尝试frege,并且正在努力尝试使用一些本机Java库。

I'm trying it out with the leiningen plugin, and Joda time. 我正在用leiningen插件和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: 显然,lein插件无法正确地为fregec查找类路径,或者可能与这种差异有关:

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 可以按预期找到Joda,而

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 根据Wiki的说法,这不应该发生

The current class path of the running JVM plus the target directory are always on the class path. 正在运行的JVM的当前类路径以及目标目录始终位于类路径上。

Still, even after manually setting the -fp , this code fails to compile: 即使手动设置了-fp ,该代码仍无法编译:

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 但是我正在使用的唯一实例方法(getYears)将引用类型作为输入( JodaYears )...我什至尝试了org.joda.time.Years ,但是编译仍然失败

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 您不能同时使用-cp和-jar调用Java

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. 您可以尝试在-cp中提供两个jar,但同时还需要说出要运行的类。 The frege compiler is frege.compiler.Main frege编译器为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: 因此,定义本机函数foo的规则是:

[ pure ] native foo XXX :: frege type [ pure ] 本机 foo XXX :: frege类型

  1. For instance methods, XXX must be a simple name. 对于实例方法, XXX必须是一个简单名称。 You can also leave XXX out, in which case it is the same as the frege name you're defining (eg foo ). 您也可以将XXX排除在外,在这种情况下,它与您要定义的徽标名称相同(例如foo )。
  2. For class methods, XXX must be the fully qualified name of the method. 对于类方法, XXX必须是方法的完全限定名称。
  3. For constructors, XXX must be "new" 对于构造函数, XXX必须是“ new”
  4. For member access, XXX must be ".member" where member is the actual member name. 对于成员访问, XXX必须是“ .member”,其中member是实际的成员名称。

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. 正如Ingo所写, years被认为是一种实例方法,因为它缺少完全限定的名称,即:我需要用另一种方式编写它。 The final working example of my helloworld code: 我的helloworld代码的最后一个工作示例:

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

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

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