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

Int value implicitly converted to String? #479

Closed
simonewebdesign opened this Issue Jan 12, 2016 · 5 comments

Comments

Projects
None yet
2 participants
@simonewebdesign

simonewebdesign commented Jan 12, 2016

I'm using evancz/elm-http to send an HTTP request, here's the code:

deleteArtist : Int -> Task Http.Error ()
deleteArtist id =
  Http.send Http.defaultSettings
    { verb = "DELETE"
    , headers = []
    , url = "http://localhost:4000/api/artists/" ++ id
    , body = Http.empty
    }
  `andThen` (\_ -> Signal.send actions.address (DeleteArtist id))
  `onError` (\error -> Signal.send actions.address NoOp)

I get a compile error:

The argument to function `DeleteArtist` is causing a mismatch.

130│                                                 DeleteArtist id)
                                                                  ^^
Function `DeleteArtist` is expecting the argument to be:

    Int

But it is:

    String

I found a workaround, which is to move the Http.Request details to its own function and pass the id to it:

deleteRequest : String -> Http.Request
deleteRequest id =
  { verb = "DELETE"
  , headers = []
  , url = "http://localhost:4000/api/artists/" ++ id
  , body = Http.empty
  }

deleteArtist : Int -> Task Http.Error ()
deleteArtist id =
  Http.send Http.defaultSettings (deleteRequest (toString id))
  `andThen` (\_ -> Signal.send actions.address (DeleteArtist id))
  `onError` (\error -> Signal.send actions.address NoOp)

I'm guessing that, in the first example, ++ is somehow changing the parameter type to String.
Is this expected behavior?
My expectation would be that the id parameter remains of type Int all the time.

Thanks in advance for any clarification.

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Jan 12, 2016

Contributor

In a type correct program, id will have only one type. But your original program is not type correct. You use id as a String in one line, but as an Int in another line.

One could wish for the compiler to give you a clearer message about this. See elm/error-message-catalog#70 and related issues.

But the main point here is that no silent conversion happens (what you feared). There is no bug in compiler or libraries here, just an imperfect error message produced by the compiler.

Contributor

jvoigtlaender commented Jan 12, 2016

In a type correct program, id will have only one type. But your original program is not type correct. You use id as a String in one line, but as an Int in another line.

One could wish for the compiler to give you a clearer message about this. See elm/error-message-catalog#70 and related issues.

But the main point here is that no silent conversion happens (what you feared). There is no bug in compiler or libraries here, just an imperfect error message produced by the compiler.

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Jan 12, 2016

Contributor

The way you should probably have written your function is:

deleteArtist : Int -> Task Http.Error ()
deleteArtist id =
  Http.send Http.defaultSettings
    { verb = "DELETE"
    , headers = []
    , url = "http://localhost:4000/api/artists/" ++ toString id
    , body = Http.empty
    }
  `andThen` (\_ -> Signal.send actions.address (DeleteArtist id))
  `onError` (\error -> Signal.send actions.address NoOp)
Contributor

jvoigtlaender commented Jan 12, 2016

The way you should probably have written your function is:

deleteArtist : Int -> Task Http.Error ()
deleteArtist id =
  Http.send Http.defaultSettings
    { verb = "DELETE"
    , headers = []
    , url = "http://localhost:4000/api/artists/" ++ toString id
    , body = Http.empty
    }
  `andThen` (\_ -> Signal.send actions.address (DeleteArtist id))
  `onError` (\error -> Signal.send actions.address NoOp)
@simonewebdesign

This comment has been minimized.

Show comment
Hide comment
@simonewebdesign

simonewebdesign Jan 12, 2016

Yes, that worked, thank you.
Looks like I got very confused by the error message, so it's probably worth improving it.

Alternatively I think would be nice to have an implicit toString call every time one tries to append some value to a String. But I don't know if that's feasible or even if makes sense at all.

simonewebdesign commented Jan 12, 2016

Yes, that worked, thank you.
Looks like I got very confused by the error message, so it's probably worth improving it.

Alternatively I think would be nice to have an implicit toString call every time one tries to append some value to a String. But I don't know if that's feasible or even if makes sense at all.

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Jan 12, 2016

Contributor

Looks like I got very confused by the error message, so it's probably worth improving it.

Yes, that's what the issue in the https://github.com/elm-lang/error-message-catalog/ repo I linked to is about.

So can this issue here be closed?

Contributor

jvoigtlaender commented Jan 12, 2016

Looks like I got very confused by the error message, so it's probably worth improving it.

Yes, that's what the issue in the https://github.com/elm-lang/error-message-catalog/ repo I linked to is about.

So can this issue here be closed?

@simonewebdesign

This comment has been minimized.

Show comment
Hide comment
@simonewebdesign

simonewebdesign Jan 12, 2016

Sure, thanks again for the help :)

simonewebdesign commented Jan 12, 2016

Sure, thanks again for the help :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment