简体   繁体   中英

Use java.util.Properties in Frege

I'm trying to use frege.java.Util module to construct a Properties instance. 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 . How could I resolve this ambiguity?

Well, this is some consequence of being able to use methods that are overloaded in 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.

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 .

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.

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