Subodh Nijsure
05/06/2021, 12:19 PMprivate 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
}
}
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
}
}
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 + '\'' +
'}';
}
}
ephemient
05/06/2021, 1:42 PMSubodh Nijsure
05/06/2021, 2:02 PMSubodh Nijsure
05/06/2021, 3:23 PMephemient
05/06/2021, 3:33 PMSubodh Nijsure
05/06/2021, 8:11 PMephemient
05/06/2021, 8:15 PM