-
Notifications
You must be signed in to change notification settings - Fork 47
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
Comments
Hi @abuffery Do you mean generally a way to parse from one type to another using |
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 "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:
In Dart, it would be universally useful for its most basic types:
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 "5"
.toIntEither("simple error message") // Right(5)
"Not a number"
.toIntEither("simple error message") // Left("simple error message") |
@abuffery I agree, this helpers may be indeed useful to add to 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 👍 |
toIntOption()
)
It would be nice and hopefully easy to add some common helpers like:
this would allow us to have clean code like:
The text was updated successfully, but these errors were encountered: