Skip to content

Instantly share code, notes, and snippets.

@LukaJCB
Created September 2, 2016 16:57
Show Gist options
  • Save LukaJCB/01861a044d2cd5dcbcd55804dc83d1a1 to your computer and use it in GitHub Desktop.
Save LukaJCB/01861a044d2cd5dcbcd55804dc83d1a1 to your computer and use it in GitHub Desktop.
//So now after letting IntelliJ convert our file to Kotlin we get this:
//... but there's an error
class ExampleActivity : AppCompatActivity() {
private var foo: String? = "Bar"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
foo = something
if (foo != null) {
doSomething(foo)
//Smart cast to 'String' is impossible,
// because 'foo' is a mutable property that could have been changed by this time
}
}
}
//In order for our call to 'doSomething' to actually be guaranteed to work we need foo to be Immutable AND Non-null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment