s

    smallufo

    8 months ago
    Does buildMap changes implementations in 1.6? I asked at kotlin lang forum but it seems that forum is inactive … The following code works well before 1.6
    fun getMap(): Map<String, String> {
        return buildMap {
          if (A) {
            put(key, "A")
          } else {
            put(key, "B")
          }
        }
      }
    but after upgrade to kotlin 1.6 , compiler complains type mismatch :
    Type mismatch.
    Required:
    Unit
    Found:
    String
    Type mismatch.
    Required:
    Unit
    Found:
    TypeVariable(V)?
    Type mismatch.
    Required:
    Unit
    Found:
    String?
    I have to change to this style :
    return buildMap {
          put(key, if (A) "A" else "B")
        }
    It is OK but not so readable. But why !?
    a

    Ayfri

    8 months ago
    What type is
    key
    ?
    ilya.gorbunov

    ilya.gorbunov

    8 months ago
    Your example works fine with Kotlin 1.6: https://pl.kotl.in/uDlx1wGR2 Could you share a complete reproducer?
    s

    smallufo

    8 months ago
    Oops , today I reopened IntelliJ and rollback to my original style and IDEA recognizes it again… Not sure why.🙄 Maybe there was something wrong inside IDEA…