ceylon
ceylon copied to clipboard
assert clause in comprehensions
I find myself wanting to drop an assert in a comprehension in place of an if where I don't want to deal with the else case. I don't want to ignore entries, I want to fail with a proper reason.
Is that something we could support?
I don't really see any good reason why not.
The only concern is it might lead people to expect that you can use assert in other sorts of expressions. But since an assert doesn't obviously have a "value", I don't see how that would be a very common problem.
Note also that we already have an existing request to support let in comprehensions.
I'm happy to add both these features to the language as soon as you guys are ready to implement backend support for them.
@tombentley let me know when you're ready to start working on this one.
Related: #3483
@gavinking wrote:
The only concern is it might lead people to expect that you can use assert in other sorts of expressions.
Has asserts in other expressions been considered? Why is it a concern? Many times I have wanted to do things like:
iterable.map((x) => assert (is String x))
iterable.map((x) => assert (is Float parseFloat(x)))
.. where assert would return the narrowed type (therefore not requiring assignment to a variable for storing the narrowed result in the second example).