#2828 How to map Java generics type to Fantom?

CretinHo Sat 1 May 2021

You can see the example here:

https://pivot.apache.org/tutorials/hello-world.html

It would be lucky for me if they use Java's Map type, because there is a Fantom Map type to map with it. The problem is they used their own Map type:

import org.apache.pivot.collections.Map;

I have no idea how to translate this:

public void startup(Display display, Map<String, String> properties)

to Fantom code.

Any attempts of mine always resulted in type mismatch error.

brian Sun 2 May 2021

The simple answer is you don't. Java generics are erased, so when working with Java code thru the Java FFI you just work with them as unparameterized classes. For example:

using [java]java.util::HashMap 

map := HashMap()
map.put("key1", "val1")
map.put("key2", "val2")
echo(map) 

Also look at the Interop class which provides utils to map b/w Fantom lists/maps and Java ones:

CretinHo Thu 17 Jun 2021

brian: The problem is this Java code:

public void startup(Display display, Map<String, String> properties)

How could I translate it to Fantom?

I have the override this method.

The parameter is explicitly said to be a Map<String, String>. I tried everything I could but it always resulted in a type not match error.

brian Thu 17 Jun 2021

With type erasure, you should just need the Java class:

using [java]java.util::Map as JMap


Void startup(Display, JMap props)

Login or Signup to reply.