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

change String.toInt and String.toFloat to return Maybe instead of Result #558

Closed
wants to merge 1 commit into from

Conversation

gyzerok
Copy link

@gyzerok gyzerok commented Apr 13, 2016

Whats done

Two little changes to the String module API.

-toInt : String -> Result String Int
+toInt : String -> Maybe Int

-toFloat : String -> Result String Float
+toFloat : String -> Maybe Float

Why?

For more consistency of API with other core modules.

String

  • uncons : String -> Maybe (Char, String)

List

  • head : List a -> Maybe a
  • tail : List a -> Maybe (List a)

Dict

  • get : comparable -> Dict comparable v -> Maybe v

Personally I was confused when saw this first time. One may say that we can't get an actual error message with maybe, but there is only one possible error here. And you don't return error for example for while trying to get list head, it's just redundant.

@eeue56
Copy link
Contributor

eeue56 commented Apr 13, 2016

You need to also change the Elm files, not just the native files.

@gyzerok gyzerok force-pushed the changes-to-string-api branch from d5bd877 to 1e2484c Compare April 13, 2016 11:37
@gyzerok
Copy link
Author

gyzerok commented Apr 13, 2016

@eeue56 yeah, you are right. Stupid me..

Now fixed.

@eeue56
Copy link
Contributor

eeue56 commented Apr 13, 2016

I also think adding some tests to https://github.com/elm-lang/core/blob/dev/tests/Test/String.elm might be worthwhile

@gyzerok gyzerok force-pushed the changes-to-string-api branch from 1e2484c to 454277a Compare April 13, 2016 11:49
@gyzerok
Copy link
Author

gyzerok commented Apr 13, 2016

Done.

@pisys
Copy link
Contributor

pisys commented Apr 19, 2016

I'd keep the Result return type because transforming a String to some number is something which can fail for various errors, not just one. It's just that the current implementation does not serve more specific error messages than the one implemented.

Additionally String.toInt and String.toFloat are different to the other functions you mentioned. The reason why they return Maybe is because there is something returned maybe. Nothing is returned in cases where there is nothing. That's not an error.

But a String which can't be turned into a number isn't nothing, it's an error. This difference has to be reflected by the type.

Since the Result can be turned into a Maybe very easily (call Result.toMaybe), I see no need to change the API. IMHO.

@gyzerok
Copy link
Author

gyzerok commented Apr 19, 2016

@pisys can you provide list of potential errors which you see so far?

@pisys
Copy link
Contributor

pisys commented Apr 19, 2016

The point is that a failng String-to-number conversion is an error and not nothing irrespective of how many or what kind of errors there could be.

@jvoigtlaender
Copy link
Contributor

jvoigtlaender commented Apr 19, 2016

@gyzerok, @pisys, you both might want to take this discussion into account. Note also that that linked issue being closed does not mean it has been resolved. In fact, it lives on as an item in https://github.com/elm-lang/core/issues/322. That item's name is:

#369 - clearly define the "use case" for Maybe and Result in APIs

I don't think it makes much sense to have the discussion that you are having here, until that general item has been dealt with and been decided on (involving Evan).

As a consequence, I'm inclined to close this PR here and to ask you to instead add mention of String.toInt and String.toFloat to the discussion at #369, so that they are taken into account when this issue is revisited via #322 at some point.

@jvoigtlaender
Copy link
Contributor

So, can I close here?

@evancz evancz closed this Apr 28, 2016
@evancz
Copy link
Member

evancz commented Apr 28, 2016

Yeah, this may be the right thing, but the decision will happen at a different time. It's an easy change, so it's not vital for it to be done via PR.

@gyzerok gyzerok deleted the changes-to-string-api branch November 7, 2017 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants