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

PositiveDouble prototype: representing a positive and finite numbers #5

Closed
wants to merge 6 commits into from

Conversation

ILIYANGERMANOV
Copy link
Collaborator

Based on #4 I've implemented a simple PositiveDouble prototype.

TL;DR;

  • Unsafe creation via PositiveDouble(5.0) or PositiveDouble.unsafe(-2.0) which throws are exception
  • Safe creation using PositiveDouble.fromDouble(a) which returns Option
  • Overloads for +, -, *, /, %
  • PositiveDouble#mapSafe extension for safely transforming the wrapped Double value
  • ArrowExactConstraintViolationError with detailed error message for easier debugging or handling programatically

* }
* ```
*/
open class ArrowExactConstraintViolationError(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make this less verbose? It's a trade-off between information and verboseness.

* }
* ```
*/
class ArrowExactPositiveDoubleConstraintError(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make this less verbose? It's a trade-off between information and verboseness.

@@ -69,3 +69,7 @@ kotlin {
}
}
}

tasks.named<Test>("jvmTest") {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests couldn't execute so I added this. Idk, if this or the invalidate & restart fixed the issues

@ILIYANGERMANOV ILIYANGERMANOV changed the title PositiveDouble prototype: represents a positive and finite numbers PositiveDouble prototype: representing a positive and finite numbers Apr 9, 2023
@pacher
Copy link
Collaborator

pacher commented Apr 9, 2023

Are you sure about operators returning Option? How would the usage look like for more complex formulas with more than one operation? (a - b) * c - d / e etc. It is a primitive, so I guess something like this should be easily doable without dozens of bind in between.
I was thinking more about something along the lines of

context(Raise<ArrowExactConstraintViolationError>)
operator fun plus(other: PositiveDouble): PositiveDouble

and dsl like positive { (a - b) * c - d / e } returning Either<ArrowExactConstraintViolationError, PositiveDouble>
Can't we do that?

Yes, I am suggesting to design it with context receivers from the start and keep experimental/ pre-release until context receivers are landed. I just don't see it any other way for primitives.

P.S. I apologize in advance, I could be totally off track here and misunderstand a lot since I did not even use arrow yet, just reading the new web site.

@ILIYANGERMANOV
Copy link
Collaborator Author

Side note: Don't forget to check #4 which our discussion issue:
Arrow Exact discussion

Feel free to share your thoughts, ideas, and opinions - all are more than welcome!

@ILIYANGERMANOV
Copy link
Collaborator Author

Are you sure about operators returning Option? How would the usage look like for more complex formulas with more than one operation? (a - b) * c - d / e etc. It is a primitive, so I guess something like this should be easily doable without dozens of bind in between. I was thinking more about something along the lines of

context(Raise<ArrowExactConstraintViolationError>)
operator fun plus(other: PositiveDouble): PositiveDouble

and dsl like positive { (a - b) * c - d / e } returning Either<ArrowExactConstraintViolationError, PositiveDouble> Can't we do that?

Yes, I am suggesting to design it with context receivers from the start and keep experimental/ pre-release until context receivers are landed. I just don't see it any other way for primitives.

P.S. I apologize in advance, I could be totally off track here and misunderstand a lot since I did not even use arrow yet, just reading the new web site.

Good point @pacher! I like the Raise idea + Either DSL 👍 Let's discuss in #4. I hope that during the week more people will join and we'll get more POVs - which is normal I'm spamming people with code on Sunday (I usually rest on Sundays but got excited about the Arrow Exact library 😄 )

@pacher
Copy link
Collaborator

pacher commented Apr 9, 2023

Also, it feels right to do overloading like

operator fun plus(other: PositiveDouble) = if (...) throw ...

context(Raise<ArrowExactConstraintViolationError>)
operator fun plus(other: PositiveDouble) = if (...) raise ...

but it is actively discouraged ... sigh

@pacher
Copy link
Collaborator

pacher commented Apr 9, 2023

And the biggest design question is what to do when mixing with regular primitives. What should be the return type of PositiveDouble - Double? Should it be PositiveDouble and fail when the result is negative? Or should it be just Double? Both seem sound with valid use cases.

@ILIYANGERMANOV
Copy link
Collaborator Author

We decided to use a more generalized design in #4 which makes this PR obsolete.

@ILIYANGERMANOV ILIYANGERMANOV mentioned this pull request May 3, 2023
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.

2 participants