And then you just pass the constructor.
# announcements
k
And then you just pass the constructor.
c
How about:
Copy code
fun <T, E : IllegalArgumentException> reqNotNull(value: T?, lazyException: () -> E): T {
	return value ?: throw lazyException()
}

var a = null
reqNotNull(a) { IllegalArgumentException("message") }
k
Even better, didn't think about that.
Is there a reason
E : IllegalArgumentException
?
c
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
😀 This happens every time...