Hey, not sure if it fits here because it's very ge...
# server
h
Hey, not sure if it fits here because it's very generic question. What is your best/idiomatic way to avoid the double bang (!!) when checking a map for a key and if it's not in there to fill it and immediately using/returning it? Example (imaginary):
Copy code
fun getDeviceUpdater(id: DeviceId): DeviceUpdater {
  if(!deviceMap.containsKey(id))
    deviceMap[id] = Device()
  return deviceMap[id].toDeviceUpdater()!!
}
I don't want to make the return type nullable.
s
h
Man what an easy answer if you know it exists. I tried describing it for a google search but came out empty. Thank you very much.
k
#getting-started
s
Top tip, gen AI tools are often better at these kinds of questions than (current) search engines. Of course, you then have to use Google to check that the answer the AI came up with is actually a real stdlib function and not just a fantastical invention 😂. But I bet ChatGPT would have no trouble with this question.
Although... I guess I'm only saying that because I knew the stdlib function existed. If I didn't know there was such a function, I wouldn't even think to ask an AI what it was called 🤔
h
Depends on how you phrase it I guess. If you ask it to "write me a function that..." then I think it wouldn't use the stdlib. I wasn't sure about the getting-started channel, but I guess it's the best for such simple questions.
Good suggestion though I used ChatGPT for some "inspiration" with generics in the past. But sometimes I forget about it. We also have CoPilot I think I can write prompts there as comments and it fills in the rest.
i just pasted your question as it was into perplexity.ai
h
Uhhh I didn't know about this one. Thank you.
c
its what I use instead of google now
h
😂
i
I also recommend checking suggestions in IntelliJ for collections, extension functions are also discoverable and you can get familiar with more content available for the type you are interested in. On JVM target solution can also be written as
Copy code
deviceMap.computeIfAbsent(id) { Device() }
👍 1
a
I have learned more in kotlin by just scrolling down to the suggested method/properties
h
Yeah usually I also find all kinds of ways by just looking at suggestions. I don't know why I didn't do it this time, I went straight to google.