Kotlin printer: ?.let for null-guarded uses of a value (#140 follow-up)

Abierto
#164 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
40/100
Tipo de issue
Nueva funcionalidad
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
java, kotlin

Línea de trabajo

Start by reading the Kotlin printer and the KotlinNullCheckShapes pattern matching mentioned in the issue, then trace how modification analysis determines whether a getter call is side-effect free. Implement the listed null-guard shapes while retaining smart casts for stable locals and parameters. Check the reported ?.let, !!, and ?. counts on langchain4j and fernflower, and confirm compile counts remain unchanged across jenkins, guava, langchain4j, fernflower, and timefold.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Follow-up of #140 (idiom scorecard: .let 0 in langchain4j's output).

Today. The Kotlin printer never emits let. A null-guarded use of a value prints as a smart cast where the value is a local or a parameter, or as !! where it is a getter call or a field, which Kotlin cannot smart-cast.

Shapes (Java → Kotlin):

Java Kotlin
x != null ? f(x) : null x?.let { f(it) }
x != null ? f(x) : d x?.let { f(it) } ?: d
if (getX() != null) use(getX()); (same pure getter) getX()?.let { use(it) }
T v = getX(); if (v != null) { … v … } (v not used after) getX()?.let { v -> … }

Why with #140's item 1. It shares the null-check pattern matching of ?./?: derived from Java's null checks (KotlinNullCheckShapes). It targets part of the !! count: the repeated nullable getter calls are about 100 of fernflower's 2,859 !! (corrected 2026-10-23, see the evaluation below; the first version called them the main source).

What decides it.

  • The getter must be the same call and side-effect free between the test and the use: the modification analysis says so; a syntactic translator cannot.
  • Not where the block returns from the enclosing function, or breaks/continues a loop. Corrected: let is inline, so a non-local return is allowed inside it, and Kotlin 2.2+ allows break/continue there too.
  • Keep the plain smart cast where the value is a stable local or parameter: if (x != null) use(x) is already idiomatic.

Measure. Counts of ?.let, !! and ?. on langchain4j and fernflower before and after; compile counts unchanged across jenkins, guava, langchain4j, fernflower and timefold.

🤖 Generated with Claude Code

Lenguaje dominante
Java
Estrellas
1
Forks
1
Merge medio
1 h 13 min
PR fusionados (30 d)
6

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de CodeLaser/maddi

Todos los issues de CodeLaser/maddi

Issues similares

Más issues de Java

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.