简体   繁体   English

在Frege中使用java.util.Properties

[英]Use java.util.Properties in Frege

I'm trying to use frege.java.Util module to construct a Properties instance. 我正在尝试使用frege.java.Util模块构造一个Properties实例。 Here's the code: 这是代码:

module frege_test.Application where

import frege.java.Util (Properties)

main :: [String] -> IO Int
main _ = do
  properties <- Properties.new ()
  return 0

That don't compile, here's the compiler error: 无法编译,这是编译器错误:

E T:\Temp\frege-test\src\main\frege\Application.fr:7: overloaded  new  is ambiguos at type  ()→IO t17332
    It could mean one of
    Properties.newα  ::  ∀ s.() → STMutable s Properties
    Properties.newβ  ::  ∀ s.Mutable s Properties → STMutable s Properties
    Util.Hashtable.newα  ::  ∀ s k v.() → STMutable s (Util.Hashtable k v)
    Util.Hashtable.newβ  ::  ∀ s k v.Int → STMutable s (Util.Hashtable k v)
    Util.Hashtable.newγ  ::  ∀ s k v.Int → Float → STMutable s (Util.Hashtable k v)
    Util.Hashtable.newδ  ::  ∀ s k v.Mutable s (Util.Map k v) → STMutable s (Util.Hashtable k v)
frege_test.Application: build failed because of compilation errors.

What's up? 这是怎么回事? I haven't even imported Util.Hashtable . 我什Util.Hashtable没有导入Util.Hashtable How could I resolve this ambiguity? 我该如何解决这种歧义?

Well, this is some consequence of being able to use methods that are overloaded in Java. 嗯,这是能够使用Java重载的方法的结果。 While this works without any issues most of the time when the overloads have the same arity, it doesn't work always without extra type annotations in the other cases. 尽管在大多数情况下重载具有相同的含义时,此方法都不会出现任何问题,但在其他情况下,如果没有额外的类型注释,它就不会总是起作用。 The more so when there are no other information available about what the variable properties should be, as in the case above. 在没有其他有关变量properties应具有的信息的情况下(如上述情况),则更是如此。

The easiest quick fix is to select the type of the overload one wanted to use right from the error message and writes 最简单的快速修复方法是从错误消息中选择要使用的过载类型并写入

properties <- (Properties.new :: () → STMutable s Properties) ()

However, when you often need an empty property list, the following would be better: 但是,当您经常需要一个空的属性列表时,以下方法会更好:

emptyProps :: ST s (Mutable s Properties)
emptyProps = Properties.new ()

This works because the type annotation gives the compiler enough information to select the correct overload. 之所以可行,是因为类型注释为编译器提供了足够的信息来选择正确的重载。 And you use it like so: 您可以像这样使用它:

main _ = do
    p <- emptyProps
    ...
    return 0

Concerning the Util.Hashtable : Since you imported frege.java.Util all data types and functions defined there are available and can be accessed with a qualified name like Util.Hashtable . 关于Util.Hashtable :自从导入frege.java.Util以来,定义的所有数据类型和函数都可用,并且可以使用限定名称(如Util.Hashtable进行访问。

For some reason, the compiler thinks that you might want of those. 由于某种原因,编译器认为您可能需要这些。 Perhaps because it knows that java.util.Properties is a subtype of java.util.Hastable. 也许是因为它知道java.util.Properties是java.util.Hastable的子类型。

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

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