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
base: dev
from

Conversation

Projects
None yet
5 participants
@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

This comment has been minimized.

Show comment
Hide comment
@eeue56

eeue56 Apr 13, 2016

Contributor

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

Contributor

eeue56 commented Apr 13, 2016

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

@gyzerok

This comment has been minimized.

Show comment
Hide comment
@gyzerok

gyzerok Apr 13, 2016

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

Now fixed.

gyzerok commented Apr 13, 2016

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

Now fixed.

@eeue56

This comment has been minimized.

Show comment
Hide comment
@eeue56

eeue56 Apr 13, 2016

Contributor

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

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

This comment has been minimized.

Show comment
Hide comment
@gyzerok

gyzerok commented Apr 13, 2016

Done.

@pisys

This comment has been minimized.

Show comment
Hide comment
@pisys

pisys Apr 19, 2016

Contributor

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.

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

This comment has been minimized.

Show comment
Hide comment
@gyzerok

gyzerok Apr 19, 2016

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

gyzerok commented Apr 19, 2016

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

@pisys

This comment has been minimized.

Show comment
Hide comment
@pisys

pisys Apr 19, 2016

Contributor

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.

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

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Apr 19, 2016

Contributor

@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.

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

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Apr 24, 2016

Contributor

So, can I close here?

Contributor

jvoigtlaender commented Apr 24, 2016

So, can I close here?

@evancz evancz closed this Apr 28, 2016

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz Apr 28, 2016

Member

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.

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 gyzerok:changes-to-string-api branch Nov 7, 2017

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