Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

android - Kotlin run scope function returning different data type

fun getStringFromSampleObject(sampleObj: SampleObj): String = sampleObj.run {
        age = 2
        title = "newTitle"
        isValid = true
    }.toString()

val returnedValue =
            Playground().getStringFromSampleObject(SampleObj(age = 1, title = "title", isValid = false))
        Log.e(TAG, "Play, returned string is$returnedValue")

I try to print the return value from the function getStringFromSampleObject() but the value of returnedValue is kotlin.Unit. I expect the string value instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use apply rather than run. It will return object. For better understanding you can refer to this link for Scope Functions


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...