Proposal: I'd love to have versions of `require` t...
# stdlib
l
Proposal: I'd love to have versions of
require
that allow for specifying a custom Exception type to throw, instead of just throwing
IllegalArgumentException
k
Those are called
if
.
💯 1
😄 2
👍 5
l
if doesn't do contract-stuff. i cannot do
if(foo == null) throw Foo()
and smart-cast foo into non-null by that
2
also, if isn't remotely as expressive as
require
l
You can write your own
require
, with the same contracts
The thing for require is that it's specifically to check for IllegalArguments, and that's why it uses
IllegalArgumentException
. Other use cases are on a per-user basis
k
If and exception throwing does smart cast.
l
didn't seem to do it for me, karel ^^'
k
Then you need to try again because it definitely does.
l
i will ;D
i know i could write my own require, but you could say that to any proposal here in the stdlib channel. you could write any stdlib-function yourself, but you shouldn't have to ;D This would be a great addition to the standard-library imo
k
So you want something like
Copy code
requireThrow(i > 0) { MyException("foo") }
instead of
Copy code
if (i <= 0) throw MyException("foo")
? That's not even shorter!
I'm not suggesting you to write your own function, I'm suggesting to just use if.
l
i know, but Leonardo was suggesting to write my own function. my big arguent agains
if
here is that it isn't as expressive, thus not as readable. when seeing
Copy code
if (verrryLongConditionThatChecksSomeData) {
  throw FooException("bar")
}
you don't immediately know that this is just validation-logic. its not bad, but it could very well be better. being short doesn't mean readable. Expressive within a given context means readable ;D