blakelee
09/18/2019, 5:38 PMlooki
09/18/2019, 5:40 PM?: throw Exception("foo")
instead, so it's handled with a descriptive exceptionblakelee
09/18/2019, 5:48 PMvar x: Int? = null
fun test(): Int {
var y = x
if (y == null) {
y = someJavaFunctionThatDoesntReturnNull()
}
return 1 + y!!
}
fun someJavaFunctionThatDoesntReturnNull(): Int = 1
pavel
09/18/2019, 5:54 PMfun test() = (x ?: someJavaFunctionThatDoesntReturnNull()) +1
blakelee
09/18/2019, 5:55 PMVlad
09/18/2019, 5:57 PM!!
is a perfectly reasonable language feature. There is nothing wrong with it. Having a blanket ban on a language construct does not sound like a thought out decisionLou Morda
09/18/2019, 6:10 PMMarko Mitic
09/18/2019, 6:14 PMgoto
is fine to use then? 😄Vlad
09/18/2019, 6:18 PM!!
that don't bring it nowhere close to the issues usually attributed to gotoColton Idle
09/18/2019, 6:25 PMfred.deschenes
09/18/2019, 6:38 PM!!
but allowing ?: throw...
streetsofboston
09/18/2019, 6:59 PM!!
is a code smell, but sometimes it is not. Outright banning it makes no sense in my opinion.
If you do, folks will do this `x ?: throw NullPointerException("...")`…. 🤷♂️tddmonkey
09/18/2019, 7:20 PMgoto
did have perfectly valid use cases in C 😄Jukka Siivonen
09/18/2019, 7:53 PMLou Morda
09/18/2019, 9:05 PMwasyl
09/18/2019, 9:36 PMI really don’t get banningTo mebut allowing!!
?: throw...
!!
is typically yeah it's null but I don't care
, while ?: throw
requires you to think why it could be null, and come up with appropriate message.
And in case being not-null is an invariant then checkNotNull
better conveys “I know it’s never null”. Similarly requireNotNull
is appropriate to tell “This parameter should not be null so you messed up this invocation”.
Anyway I don’t think I’ve seen legitimate usage of !!
other than being lazy and not wanting to provide message/refactor code a bit. It’s mostly people doing if (field != null) field!!.doSomething()
too, so I also get why it’s difficult for some to see why it’d be useful.
I wonder if Kotlin would have !!
operator if it wasn’t for Java interoppavel
09/18/2019, 9:39 PMval foo:Int = listOf(fooService.getFoo(), barService.getBar(), bazService.getBaz()).max()!!
wasyl
09/18/2019, 9:53 PM!!
. Personally I’d still write ``listOf(…).max().let(::checkNotNull)` but I guess this just proves the point that some people just don’t like !!
🙂
Anyway I think forbidding !!
also serves junior developers, since typically having to use it means there’s something iffy with your code (like the field I mentioned before)pavel
09/18/2019, 9:59 PM!!
can be expressed better in other ways. And having a general rule against that operator is a good idea.
But, this rule, just like most other rules, has exceptionsgildor
09/19/2019, 12:04 AMmaxOf
functionpavel
09/19/2019, 12:16 AMyen
09/19/2019, 12:57 AMmax()
on a list that is never empty) where you the programmer knows that some thing cannot be null, but the compiler can’t prove it!!
becomes necessary?: throw Exception(...)
solution for cases like thesegildor
09/19/2019, 1:31 AM?: error(...)
In this particular case, yes. But of I had 4 things or 5?Create function
maxOf
with vararg argument. I actually surprised that its’ not there
In general it looks as a good example when “I know it’s not null, give me my double bang” should be replaced with more efficient and much more idiomatic function (from stdlib or your own)
Even super simple extension like maxOrThrow()
would be better because provides much better error messageStephan Schroeder
09/19/2019, 12:15 PMfun test(): Int {
val y:Int = x ?: someJavaFunctionThatDoesntReturnNull()
return 1 + y
}
Can’t you always assign a platform type to a non-nullable type, as long as you make the type of the variable explicit!?!gildor
09/19/2019, 11:26 PMColton Idle
09/20/2019, 3:33 AMalsoI'm unfamiliar with that. What's error()??: error(...)
gildor
09/20/2019, 5:22 AMkotlin
so doesn’t require import:
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/error.htmlColton Idle
09/25/2019, 1:47 PM