https://kotlinlang.org logo
Title
k

karelpeeters

09/01/2017, 11:44 AM
And then you just pass the constructor.
c

Czar

09/01/2017, 11:52 AM
How about:
fun <T, E : IllegalArgumentException> reqNotNull(value: T?, lazyException: () -> E): T {
	return value ?: throw lazyException()
}

var a = null
reqNotNull(a) { IllegalArgumentException("message") }
k

karelpeeters

09/01/2017, 11:53 AM
Even better, didn't think about that.
Is there a reason
E : IllegalArgumentException
?
c

Czar

09/01/2017, 11:55 AM
yes, I only want to use this for particular hierarchy of exceptions in my module and the fun iteslf will be defined in that module. IAE is for illustration, actual exception is of course something like MyDomainStuffException.
Hm...
reqNotNull(a) { IllegalArgumentException("message") }
vs
a ?: throw IllegalArgumentException("message")
of course this is not generic, fits only null check, not other preconditions
k

karelpeeters

09/01/2017, 12:03 PM
😀 This happens every time...