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.

  • uranibaba@lemmy.world
    link
    fedilink
    English
    arrow-up
    1
    ·
    8 months ago

    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?

    • mea_rah@lemmy.world
      link
      fedilink
      English
      arrow-up
      2
      ·
      8 months ago

      You always get a Result. On that result you can call result.unwrap() (give me the bool or crash) or result.unwrap_or_default() (give me bool or false 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.

      • uranibaba@lemmy.world
        link
        fedilink
        English
        arrow-up
        2
        ·
        8 months ago

        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.

        The difference being where you handle the error?

        It sounds to me like Java works in kinda the same way. You either use throws Exception and require the caller to handle the exception when it occurs, or you handle it yourself and return whatever makes sense when that happens (or whatever you want to do before you do a return). The main difference being how the error is delivered.

        Java has class similar to Result called Optional.

      • kameecoding@lemmy.world
        link
        fedilink
        English
        arrow-up
        1
        arrow-down
        2
        ·
        edit-2
        8 months ago

        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

    • kattenluik@feddit.nl
      link
      fedilink
      English
      arrow-up
      1
      ·
      edit-2
      8 months ago

      Here’s some examples written on my phone:

      match result {
          Ok(bool_name) => whatever,
          Err(error_type) => whatever,
      }
      
      if let Ok(bool_name) = result {
          whatever
      }
      
      if result.is_ok() {
          whatever
      }
      
      let whatever = result.unwrap_or_default();
      let whatever = result?;
      

      And there’s many other awesome ways to use a Result including turning it into an Option or unwrapping it unsafely. I recommend you just search “Rust book” on your search engine and browse it. Here’s the docs to the Result enum.

      • uranibaba@lemmy.world
        link
        fedilink
        English
        arrow-up
        1
        ·
        8 months ago

        Ah, so it is like a wrapper enum, ok contains the data type you want and err the error object?

        • kattenluik@feddit.nl
          link
          fedilink
          English
          arrow-up
          1
          ·
          8 months ago

          Exactly! The other wrapper enum I named (Option) is the same kind of concept but with Some(value) and None.