M
Modular12mo 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
sora12mo ago
I guess if we have ADT, at least we can have our own Results and Maybes
gryznar
gryznarOP12mo ago
We could, but support from language syntax will be very handy in such case
ksandvik
ksandvik12mo 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
TausugRareBreed12mo 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 Kirchhoff12mo 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 Kirchhoff12mo ago
Maybe. I am not involved with the planning here.
ksandvik
ksandvik12mo ago
Well, we have traits now so that will be a nice addition for any future standardization of error handling.
Melody Daniel
Melody Daniel12mo ago
Mojo will still require a way to throw exceptions/ panic
Alex Kirchhoff
Alex Kirchhoff12mo ago
Yes, this is how exceptions are currently implemented.
Want results from more Discord servers?
Add your server