Two nice tips raised by @rexwangcc:
- It seems we've been using
assertoverraise Exceptionacorss the codebase, while they works similar in most cases,assertstatements are removed when the Py compilation is optimized (e.g. python -O examples/*.py), and thus assertions will be unsafely bypassed. Do we have special considerations about why we choseasserts?- For the places that are raising exceptions, I noticed often wild
Exceptionis being thrown, which might not be a good practice to me. As we start to discuss refactor the operations and math libs, it might be a good chance to have more explicit Exceptions defined inexception.py?
There seems to be a few things to decide:
asserts in non-testing code?Exceptions, so that users have clearer error messages?Exception such as ValueError, do we need to define a set of customized exceptions such as TaichiValueError in https://github.com/taichi-dev/taichi/blob/master/python/taichi/lang/exception.py?Discussions are welcome!
My personal opinions: Yes, Yes, No (we should first try to reuse standard Python exceptions).
I think the distinction between assertions and exceptions are: whether this is a violation of the system invariants, or an error possibly due to something we cannot control (e.g. user input error, OOM, etc). In another word, assertions are something that should never fail. Otherwise we should throw an exception.
...thus assertions will be unsafely bypassed
Good point. assert() in C/C++ could also be compiled away in release build. So I think an implication is that we should never put any expression with side-effects inside any sort of assertions in any language.
Agreed. I may be of help in this, as I'm currently refactoring the matrix and math api, where I've found some of the error messages (resulted from incorrect usage) to be really cryptic.....
Most helpful comment
Agreed. I may be of help in this, as I'm currently refactoring the matrix and math api, where I've found some of the error messages (resulted from incorrect usage) to be really cryptic.....