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

Implement built-in conversions to Option and Either (e.g. toIntOption()) #80

Closed
abuffery opened this issue Jan 14, 2023 · 3 comments · Fixed by #110
Closed

Implement built-in conversions to Option and Either (e.g. toIntOption()) #80

abuffery opened this issue Jan 14, 2023 · 3 comments · Fixed by #110
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@abuffery
Copy link

It would be nice and hopefully easy to add some common helpers like:

Option<int> toIntOption(String x) => Option.tryCatch(() => int.parse(x));

this would allow us to have clean code like:

assert(Right("1985").map(toIntOption) == Right(Some(1985)));
@SandroMaglione
Copy link
Owner

Hi @abuffery

Do you mean generally a way to parse from one type to another using fpdart (Option)? Do you have some idea for other helpers or toIntOption would be enough?

@SandroMaglione SandroMaglione added the enhancement New feature or request label Jan 16, 2023
@abuffery
Copy link
Author

Hi @SandroMaglione

I am taking as reference Scala that has built-in conversions to Option.

In Scala you can do "41a".toInt and also "41a".toIntOption.

This leads to a leaner syntax, specially when chaining.

For example "abc".toIntOption.getOrElse(-1) is easy on the eyes to follow and read.

"5".toIntOption                 // Option[Int] = Some(5)
"abc".toIntOption               // Option[Int] = None
"abc".toIntOption.getOrElse(-1) // Int = -1

I generally find it even clearer when piped on separate lines.

"abc"
  .toIntOption
  .getOrElse(-1) // Int = -1

Other Scala conversions to Option:

  • toLongOption
  • toDoubleOption
  • toBooleanOption
  • toCharOption
  • toByteOption
  • toShortOption

In Dart, it would be universally useful for its most basic types:

  • toBoolOption
  • toDoubleOption
  • toIntOption
  • toStringOption

Scala does not have a toIntEither and at the same time it may make sense for common basic types.

Currently fpdart has for elaborate cases:

final tryCatch = Either.tryCatch(
  () => int.parse('invalid'),
  (e, s) => 'Error: $e',
);

Why not add to fpdart helpers for basic types when one wants returned a Left<String> instead of None

"5"
  .toIntEither("simple error message")  //  Right(5)

"Not a number"
  .toIntEither("simple error message")  //  Left("simple error message")

@SandroMaglione
Copy link
Owner

@abuffery I agree, this helpers may be indeed useful to add to fpdart, both for Option and Either.

I add this to the list of features to add to the library, using this as reference issue for further discussion if necessary 🔜

Thanks for the suggestions 👍

@SandroMaglione SandroMaglione self-assigned this Jan 23, 2023
@SandroMaglione SandroMaglione added the good first issue Good for newcomers label Jan 23, 2023
@SandroMaglione SandroMaglione changed the title toIntOption() Implement built-in conversions to Option and Either (e.g. toIntOption()) Apr 28, 2023
@SandroMaglione SandroMaglione linked a pull request May 18, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants