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

NonEmptyString type to handle empty string cases in the code #697

Closed
ondrejsevcik opened this Issue Aug 23, 2016 · 2 comments

Comments

Projects
None yet
3 participants
@ondrejsevcik

ondrejsevcik commented Aug 23, 2016

Hi, sometimes I have to treat an empty string as an invalid state (e.g. in Username) and in those cases, I have to check for all occurrences in my code and make sure that there is proper check for an empty string value.

It would be really great if the compiler could do all the checks for me. I thought about NonEmptyString type that would always contain some characters.

username : NonEmptyString
username = "Donald Duck" -- valid
username = "" -- compile error

then I could define my username to Maybe NonEmptyString and the compiler will make sure that all the cases are handled in my code.

username : Maybe NonEmptyString
username = Just "Donald Duck"

...

case username of 
    Just username ->
        ...
    Nothing ->
        ...

What do you think about it?
How do you currently solve this issue in your code?

@process-bot

This comment has been minimized.

Show comment
Hide comment
@process-bot

process-bot Aug 23, 2016

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

process-bot commented Aug 23, 2016

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz Aug 23, 2016

Member

I think it makes sense to do more discussion on Elm Slack or elm-discuss before having an issue here about it.

I often use languages with a NonEmptyList type and it's never been worth the trouble in my experience. I.e. I've never had a bug related to doing this wrong.

One way to handle your case is to make a special type:

type UserName = Known String | Unknown

user : String -> Result String UserName

And you can have the user function check that everything is valid. If the internal details of User are hidden, no one on the outside can ever modify the name, so you know that all User data is good and only have to check once.

In any case, makes sense to continue on elm-discuss or Slack.

Member

evancz commented Aug 23, 2016

I think it makes sense to do more discussion on Elm Slack or elm-discuss before having an issue here about it.

I often use languages with a NonEmptyList type and it's never been worth the trouble in my experience. I.e. I've never had a bug related to doing this wrong.

One way to handle your case is to make a special type:

type UserName = Known String | Unknown

user : String -> Result String UserName

And you can have the user function check that everything is valid. If the internal details of User are hidden, no one on the outside can ever modify the name, so you know that all User data is good and only have to check once.

In any case, makes sense to continue on elm-discuss or Slack.

@evancz evancz closed this Aug 23, 2016

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