Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest unwrapping when passing Result(a, _) instead of a #3644

Open
giacomocavalieri opened this issue Sep 25, 2024 · 8 comments · May be fixed by #3654
Open

Suggest unwrapping when passing Result(a, _) instead of a #3644

giacomocavalieri opened this issue Sep 25, 2024 · 8 comments · May be fixed by #3654
Labels
help wanted Contributions encouraged priority:medium

Comments

@giacomocavalieri
Copy link
Member

It would be nice if the compiler could suggest how to deal with a result when it's being used where the wrapped Ok value is expected instead. What I mean is:

pub fn main() {
  let response =
    request.new()
    |> request.set_host("pokeapi.co")
    |> request.set_path("api/v2/pokemon/ditto")
    |> httpc.send

  parse_response(response)
}

fn parse_response(response: Response(String)) {
  todo
}

Have an error that looks like this:

error: Type mismatch
   ┌─ /Users/giacomocavalieri/Desktop/goto/src/goto.gleam:13:18
   │
13 │   parse_response(response)
   │                  ^^^^^^^^

Expected type:

    Response(String)

Found type:

    Result(Response(String), Dynamic)

Hint: to get a value out of a `Result` you can pattern match on it

    case response {
      Ok(response) -> parse_response(response)
      Error(_) -> todo as "deal with error"
    }

We could also have a code action to apply this fix too.

@lpil
Copy link
Member

lpil commented Sep 28, 2024

Sounds good! I'm not sure this is the right place for a code action though as most the time you don't want to use case, and there's an action for expanding case

@lpil lpil added help wanted Contributions encouraged priority:medium labels Sep 28, 2024
@GearsDatapacks
Copy link
Contributor

Could this apply the other way round too? For example:

fn fallible(value: String) -> Result(Int, Nil) {
  case value {
    "1" -> 1
        // ^ The compiler could suggest wrapping this in `Ok`
    "2" -> 2
     _ -> int.parse(value)
  }
}

Rust does this, for example

@lpil
Copy link
Member

lpil commented Sep 29, 2024

I think that's a great idea but in that specific case there wouldn't be an error for that clause.

@GearsDatapacks
Copy link
Contributor

Yes that's true, probably a bad example. Should I open a separate issue for that?

@lpil
Copy link
Member

lpil commented Sep 30, 2024

For what, sorry?

@GearsDatapacks
Copy link
Contributor

My suggestion above

@lpil
Copy link
Member

lpil commented Sep 30, 2024

Sorry, I'm a bit confused. There's no type error on that line. How would it work?

@GearsDatapacks
Copy link
Contributor

GearsDatapacks commented Sep 30, 2024

In a different case for example:

pub fn wibble() -> Result(Int, Nil) {
  10
//^ here for example
}

Maybe that's not particularly useful though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Contributions encouraged priority:medium
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants