Skip to content

Instantly share code, notes, and snippets.

@gelin
Created February 20, 2020 13:35
Show Gist options
  • Save gelin/2aa987b96cfd888e5308c3453496ac85 to your computer and use it in GitHub Desktop.
Save gelin/2aa987b96cfd888e5308c3453496ac85 to your computer and use it in GitHub Desktop.
Type inference in Kotlin for Android
//Java
newIntent.setData(intent.getParcelableExtra(Intent.EXTRA_STREAM)); //compilation error
newIntent.setData(intent.<Uri>getParcelableExtra(Intent.EXTRA_STREAM)); //explicit type declaration
newIntent.setData((Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM)); //explicit type cast
//Kotlin
newIntent.setData(intent.getParcelableExtra(Intent.EXTRA_STREAM)) //implicit type inference!
val stream = intent.getParcelableExtra(Intent.EXTRA_STREAM) //compilation error
val stream : Uri = intent.getParcelableExtra(Intent.EXTRA_STREAM) //explicit type inference
newIntent.setData(stream)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment