Explain Kotlin code

Paste Kotlin and Vibe explains where null can turn up, what a data class writes for you and where an extension function comes from.

Parcel.kt

Up to 1,500 characters (about 60 lines). Your code is sent to Unvibe and Google Gemini to write the explanation. Don't paste keys or passwords. Common key formats are refused.

Vibe12 lines selected

Press Explain it for the sample, or paste your own code first.

What this Kotlin sample shows

null safety
note is a String?, so it may be null, and it is unless you pass one. note?.trim() calls trim only when there is a note, and ?: "no note" fills in when there isn't. Write note.trim() and the compiler stops: only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type String?.
extensions
fun Parcel.label() and fun Parcel.withExtra() add functions to Parcel from outside the class, so packingLabels can call it.label() as if it were a method. Inside them, code, grams, note and copy belong to the parcel they were called on.
data classes
data class writes equals, copy and destructuring for the three properties. withExtra(25) returns a 525 g copy and leaves the 500 g original as it was, original == original.copy() is true, and val (code, grams, note) = original gives BOX, 500 and null.

Good to know

  • packingLabels turns a 500 g BOX without a note and a 100 g BAG whose note is fragile with two spaces on each side into BOX: 500 g (no note) and BAG: 100 g (fragile).
  • withExtra changes only grams: the copy keeps the original's code and note, and the two parcels are not equal.
  • An empty list of parcels gives an empty list of labels.

Watch out: ?: only steps in for null. A note of three spaces trims to an empty string, which isn't null, so a 20 g ENV parcel with that note gets the label ENV: 20 g (). note?.trim()?.ifBlank { null } ?: "no note" gives no note for it.

Explain Kotlin code beside your editor

Unvibe is a free app for Mac and Windows. In the app there is no pasting: select code in any app and press Control+U (Ctrl+Alt+U on Windows).