Hello,
I’m playing a bit with Hazelcast at the moment using Kotlin 8.11:
val cfg = Config(); val instance = Hazelcast.newHazelcastInstance(cfg); val mapCustomers: IMap<Int, String>? = instance?.getMap("customers"); val myMap = mapCustomers as java.util.Map<Int, String>
if(myMap != null) {
myMap.put(1, “Joe”);
myMap.put(2, “Ali”);
myMap.put(3, “Avi”);println("Customer with key 1: "+ myMap.get(“1”));
println(“Map Size:” + myMap.size());
}
As the code shows in line 3 the message instance?.getMap(“customers”) returns an Hazelcast type named IMap which implements java.util.Map. To get things to compile I introduced the helper variable myMap. My first question is whether there is a more elegant way to get around having to declare that helper map myMap. Everything I tried didn’t work, f.ex.
val mapCustomers = (instance?.getMap("customers")) as HashMap<Int, String>;
But my main question concerns this piece of code:
var mapCustomers: IMap<Int, String>? = instance?.getMap("customers"); mapCustomers = null; val myMap = mapCustomers as java.util.Map<Int, String>
The code above compiles and runs and of course creates a kotlin.TypeCastException in the 3rd line. My question is whether the compiler shouldn't have complained that it must be
?
instead of
mapCustomers as java.util.Map<Int, String> // no question mark
because it knows that mapCustomers in the 1st line was declared with an ?
Regards, Oliver