Skip to content

Instantly share code, notes, and snippets.

@christianb
Last active June 1, 2019 16:31
Show Gist options
  • Save christianb/2474bac997ef21812afefb2d12b7723d to your computer and use it in GitHub Desktop.
Save christianb/2474bac997ef21812afefb2d12b7723d to your computer and use it in GitHub Desktop.
Carfefull with destructuring declaration
// A problem like destructuring declarations
// by Maria Neumayer
// https://medium.com/a-problem-like-maria/a-problem-like-destructuring-declarations-95f10375ea8a
data class Song(
val artist: String,
// val album: String, // once this line gets uncommented
val name: String
)
// this line of code will still compile, but instead of "name" it contains the field of album, no compiler warning !
val (artist, name) = song
// Because compiled code of a destructuring declaration will call the componentN functions on the class
val artist = song.component1()
val name = song.component2() // clash with "album" !
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment