I created this issue here - <https://youtrack.jetb...
# announcements
s
I created this issue here - https://youtrack.jetbrains.com/issue/KT-46508 but thought might get better guidance here. Consider main.kt and Attachment.java below, should the compiler have warned about self-assignment in extension method below?
Copy code
private fun ImageFile.toJavaAttachment(): JavaAttachment {
    return JavaAttachment().apply {
        /* Here imageName is correctly referenced */
        fileName = imageName
        /* compiler should have warned that this is a self-assignment? */
        updateTime = updateTime
    }
}
Copy code
package org.kotllinlang.play

import JavaAttachment

data class ImageFile (
    val imageName: String,
    val imageType: Int,
    val updateTime: String
)

data class KotlinAttachment (
    var fileName: String = "default-kotlin-file-name",
    var updateTime: String = "default-kotlin-time"
)

fun main() {

    val image = ImageFile(
        imageName = "file.jpg",
        imageType = 1,
        updateTime = "this-is-updated-time-stamp")

    val javaAttachment = image.toJavaAttachment()

    val kotlinAttachment = image.toKotlinAttachment()


    println("Java attachment $javaAttachment")

    println("Kotlin attachment $kotlinAttachment")

}

private fun ImageFile.toKotlinAttachment(): KotlinAttachment {
    return KotlinAttachment().apply {
        fileName = imageName
        /* compiler correctly generates a self-assignment warning */
        updateTime = updateTime
    }
}

private fun ImageFile.toJavaAttachment(): JavaAttachment {
    return JavaAttachment().apply {
        /* Here imageName is correctly referenced */
        fileName = imageName
        /* compiler should have warned that this is a self-assignment? */
        updateTime = updateTime
    }
}
Copy code
package org.kotllinlang.play;
public class JavaAttachment {

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }

    private String fileName;
    private String updateTime;

    public JavaAttachment() {
        updateTime = "default-java-time";
        fileName = "default-java-file-name";
    }

    @Override
    public String toString() {
        return "JavaAttachment{" +
                "fileName='" + fileName + '\'' +
                ", updateTime='" + updateTime + '\'' +
                '}';
    }
}
e
no, why should it? it has no idea if the accessors have desired side effects or not.
s
well there are lot of things that compilers typically do, that they can technically get away by (glib) argument "why should it"
I updated problem statement, to show that compiler/ide shows warning for Kotlin class.
e
if it's a Kotlin class within the same module, it can see sources and understand that there is no further logic behind the getters and setters. otherwise, it can't.
s
Don't know if you noticed one class is JAVA class where compiler is not able to warn about self-assignment and other is a Kotlin data class in which case compiler is able to warn. Both classes are in same package, so I don't think class "visibility" should be an issue.
e
it's not by package name, it's by module boundaries. if you have a Kotlin class in a different Gradle project it wouldn't either.