M
Modular13mo ago
gryznar

Error handling like in Rust

Is Mojo going to have error handling like in Rust? With OK an Error Result exposed to the user? Or will it have more traditional way to handle errors, like in Python?
10 Replies
sora
sora13mo ago
I guess if we have ADT, at least we can have our own Results and Maybes
gryznar
gryznarOP13mo ago
We could, but support from language syntax will be very handy in such case
ksandvik
ksandvik13mo ago
I don't know about the future of error handling Mojo but so far trying to be 1:1 with Python exceptions is a goal. Meanwhile this is an interesting article how to combine the best from Go and Rust into a more Pythonic error solution: https://www.inngest.com/blog/python-errors-as-values
TausugRareBreed
TausugRareBreed13mo ago
I suspect it will need both. Once mojo gets Algebraic Data Types, you can create your own Sum types like rust's Option and Result. On the other for python compatibility, I would imagine mojo will need some form of Exception handling. If a cpython function is called and it throws an exception, I'm not sure how mojo would handle this. This also begs the question of other FFI boundary related questions (will mojo have a unsafe dual world?).
Alex Kirchhoff
Alex Kirchhoff13mo ago
If a cpython function is called and it throws an exception, I'm not sure how mojo would handle this.
Mojo supports try/except on functions decorated with the raises keyword (see e.g. https://docs.modular.com/mojo/manual/basics.html#python-integration), and this is used for the Python interop. This is a rather primitive mechanism at present. I am not familiar with plans for the future of error handling in Mojo.
Modular Docs - Language basics
Introduction to Mojo's basic language features.
Maximum Limelihood Estimator
Important to distinguish exceptions from errors, I think. I wonder if Algebraic Effects Handling/Conditions are on the table?
Alex Kirchhoff
Alex Kirchhoff13mo ago
Maybe. I am not involved with the planning here.
ksandvik
ksandvik13mo ago
Well, we have traits now so that will be a nice addition for any future standardization of error handling.
Melody Daniel
Melody Daniel13mo ago
Mojo will still require a way to throw exceptions/ panic
Alex Kirchhoff
Alex Kirchhoff13mo ago
Yes, this is how exceptions are currently implemented.
Want results from more Discord servers?
Add your server