wouldnt such a syntax be more convenient? ```val ...
# language-proposals
m
wouldnt such a syntax be more convenient?
Copy code
val (first, second!!) = Pair<String, String?>("not null", "same")

// so I dont have to:
val (first, second) = Pair<String, String?>("not null", "same")
// and then
val secondNotNull = second!!
f
Too esoteric to complicate the language. If the return value is never null it should not be marked as being nullable, and if it can be null then the caller should not ignore it.
k
Instead of
val secondNotNull = second!!
, all you have to do is
second!!
by itself on the next line, and then just use
second
(smart cast). Alternatively,
val (first, second) = foo() as Pair<String, String>
but you'll get a warning.
m
@Fleshgrinder The value IS null when the special flag is set to false. But it is NEVER null when the flag is set to true
@Klitos Kyriacou Yeah, right. But the problem is that I don't want to use
!!
each time I access the variable. It's not a big problem to consume a nullable value and use
!!
once.
k
That's what I'm saying - just use
second!!
once. Not every time you access it.
2
m
oh.. smart casts
Thank you all.
f
The same works with
requireNotNull
and
checkNotNull
and any other function that defines an appropriate contract:
Copy code
val (first, second) = f() // Pair<String, String?>

second!!
requireNotNull(second) { "imposibru!" }
checkNotNull(second) { "impsibru!" }

second // definitely not null here 8)
👍 2
m
Yeah, I just remembered
f
Sounds like contracts could help