- cross-posted to:
- fediverse@lemmy.ml
- cross-posted to:
- fediverse@lemmy.ml
Seems like an interesting effort. A developer is building an alternative Java-based backend to Lemmy’s Rust-based one, with the goal of building in a handful of different features. The dev is looking at using this compatibility to migrate their instance over to the new platform, while allowing the community to use their apps of choice.
That i dont understand? How can it be a result that i need to handle? If its not correct than java will throw an error. ( As expected, shit in shit out )
It’s a great and probably the best error system I’ve seen, instead of just throwing errors and having bulky try catch statements and such there’s just a result type.
Say you have a function that returns a boolean in which something could error, the function would return a Result<bool, Error> and that’s it. Calling the function you can choose to do anything you want with that possible Error, including ignoring it or logging or anything you could want.
It’s extremely simple.
If I except a boolean, there is an error and get a Result, is Result an object? How do I know if I get a bool or error?
You always get a Result. On that result you can call
result.unwrap()
(give me the bool or crash) orresult.unwrap_or_default()
(give me bool orfalse
if there was error) or any other way you can think of. The point is that Rust won’t let you get value out of that Result until you somehow didn’t handle possible failure. If function does not return Result and returns just value directly, you (as a function caller) are guaranteed to always get a value, you can rely on there not being a failure that the function didn’t handle internally.That’s a kinda terrible way to do it compared to letting it bubble up to the global error handler.
You can also use optional in java if you want a similar pattern but that only makes sense for stuff where it’s not guaranteed that you get back the data you want such as db or web fetch