Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
Map.computeIfAbsent requires not null result in Kotlin

Code snippet

val map = mutableMapOf<String, String>()
map.computeIfAbsent(key) { calcValue(it) }

with calcValue signature:

fun calcValue(key: String): String?

compiles with error:

Type mismatch: inferred type is String? but String was expected

But it is acceptable for mappingFunction to return null in Map.computeIfAbsent.

Is this a bug? Or am I doing something wrong?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

This was apparently explicitely not allowed by the kotlin team to simplify the usage. See explanation here: https://youtrack.jetbrains.com/issue/KT-10982#focus=streamItem-27-2187715.0-0

So either use compute or use a nullable type as value in the map.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!