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

Binary operations for AnyInt hierarchy #31

Closed
7 tasks done
LVMVRQUXL opened this issue Feb 1, 2023 · 10 comments · Fixed by #41
Closed
7 tasks done

Binary operations for AnyInt hierarchy #31

LVMVRQUXL opened this issue Feb 1, 2023 · 10 comments · Fixed by #41
Assignees
Labels
common Item related to all platforms. feature New feature or request.
Milestone

Comments

@LVMVRQUXL
Copy link
Contributor

LVMVRQUXL commented Feb 1, 2023

Description

Implement binary operations for the AnyInt hierarchy.

Checklist

  • Implement the plus operations.
  • Implement the minus operations.
  • Implement the times operations.
  • Implement the div operations.
  • Implement the rem operations.
  • Refactor code.
  • Update the Unreleased section in changelog.
@LVMVRQUXL LVMVRQUXL self-assigned this Feb 1, 2023
@LVMVRQUXL LVMVRQUXL added feature New feature or request. common Item related to all platforms. labels Feb 1, 2023
@LVMVRQUXL LVMVRQUXL added this to the 4.1.0 milestone Feb 1, 2023
@LVMVRQUXL LVMVRQUXL changed the title Binary operations of integers Binary operations for AnyInt hierarchy Feb 1, 2023
@LVMVRQUXL LVMVRQUXL removed their assignment Feb 1, 2023
@LVMVRQUXL
Copy link
Contributor Author

LVMVRQUXL commented Feb 2, 2023

We should also introduce binary operations using the Int type as a parameter or receiver for a better integration with the standard library.

@LVMVRQUXL
Copy link
Contributor Author

Here is the list of plus operations to implement:

/** Adds the [other] integer to this one. */
operator fun Int.plus(other: AnyInt): Int
operator fun AnyInt.plus(other: Int): Int
operator fun AnyInt.plus(other: AnyInt): Int

@LVMVRQUXL
Copy link
Contributor Author

Here's the list of minus operations to implement:

/** Subtracts the [other] integer from this one. */
operator fun Int.minus(other: AnyInt): Int
operator fun AnyInt.minus(other: Int): Int
operator fun AnyInt.minus(other: AnyInt): Int

@LVMVRQUXL
Copy link
Contributor Author

Here's the list of times operations to implement:

/** Multiplies this integer by the [other] one. */
operator fun Int.times(other: AnyInt): Int
operator fun AnyInt.times(other: Int): Int
operator fun AnyInt.times(other: AnyInt): Int

@LVMVRQUXL
Copy link
Contributor Author

Here's the list of div operations to implement:

/**
 * Divides this integer by the [other] one, truncating the result to an integer
 * that is closer to [zero][ZeroInt].
 */
operator fun Int.div(other: NonZeroInt): Int
operator fun AnyInt.div(other: NonZeroInt): Int
operator fun PositiveInt.div(other: StrictlyPositiveInt): PositiveInt
operator fun PositiveInt.div(other: StrictlyNegativeInt): NegativeInt
operator fun NegativeInt.div(other: StrictlyPositiveInt): NegativeInt
operator fun NegativeInt.div(other: StrictlyNegativeInt): PositiveInt

@LVMVRQUXL
Copy link
Contributor Author

Here's the list of rem operations to implement:

/**
 * Calculates the remainder of truncating division of this integer by the
 * [other] one.
 */
operator fun Int.rem(other: NonZeroInt): Int
operator fun AnyInt.rem(other: NonZeroInt): Int
operator fun PositiveInt.rem(other: NonZeroInt): PositiveInt
operator fun NegativeInt.rem(other: NonZeroInt): NegativeInt

@LVMVRQUXL
Copy link
Contributor Author

Those changes will not be implemented according to our design principles.

@LVMVRQUXL LVMVRQUXL removed this from the 4.1.0 milestone Feb 12, 2023
@LVMVRQUXL
Copy link
Contributor Author

Implementing those changes will simplify the usage of the AnyInt hierarchy.

val x: NonZeroInt = NonZeroInt.random()
val y: PositiveInt = PositiveInt.random()
var result: Int
// before
result = x.toInt() + 1
result = 1 + x.toInt()
result = x.toInt() + y.toInt()
// after
result = x + 1
result = 1 + x
result = x + y

@LVMVRQUXL LVMVRQUXL reopened this Feb 12, 2023
@LVMVRQUXL LVMVRQUXL removed the wontfix label Feb 12, 2023
@LVMVRQUXL LVMVRQUXL added this to the 4.1.0 milestone Feb 12, 2023
@LVMVRQUXL LVMVRQUXL removed this from the 4.1.0 milestone Feb 12, 2023
@LVMVRQUXL LVMVRQUXL reopened this Feb 12, 2023
@LVMVRQUXL LVMVRQUXL removed the wontfix label Feb 12, 2023
@LVMVRQUXL LVMVRQUXL added this to the 4.1.0 milestone Feb 12, 2023
@LVMVRQUXL LVMVRQUXL reopened this Mar 5, 2023
@LVMVRQUXL LVMVRQUXL removed the wontfix label Mar 5, 2023
LVMVRQUXL added a commit that referenced this issue Mar 7, 2023
This operation was converted to a member function and now returns an AnyInt instead of an Int.
@LVMVRQUXL LVMVRQUXL added this to the 4.1.0 milestone Mar 15, 2023
LVMVRQUXL added a commit that referenced this issue Mar 10, 2024
LVMVRQUXL added a commit that referenced this issue Mar 10, 2024
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
common Item related to all platforms. feature New feature or request.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant