Skip to content

Commit

Permalink
feat(#31): add plus operations for AnyInt
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Mar 15, 2023
1 parent 5a86e68 commit 36af7a3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
23 changes: 20 additions & 3 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,33 @@ All notable changes to this project will be documented in this file.

### Added

Introduce a new `NotEmptyCollection` hierarchy representing collections that
contain at least one element (issue
[#14](https://github.com/kotools/types/issues/14)).
- Introduce a new `NotEmptyCollection` hierarchy representing collections that
contain at least one element (issue
[#14](https://github.com/kotools/types/issues/14)).

```kotlin
interface NotEmptyCollection<out E>
class NotEmptyList<out E> : NotEmptyCollection<E>
class NotEmptySet<out E> : NotEmptyCollection<E>
```

- Binary operations (`plus`) for the `AnyInt` hierarchy (issue
[#31](https://github.com/kotools/types/issues/31)).

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

### Changed

- Support for
Expand Down
12 changes: 12 additions & 0 deletions src/commonMain/kotlin/kotools/types/number/AnyInt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ public sealed interface AnyInt {
override fun toString(): String
}

/** Adds the [other] integer to this one. */
@SinceKotoolsTypes("4.1")
public operator fun Int.plus(other: AnyInt): Int = this + other.toInt()

/** Adds the [other] integer to this one. */
@SinceKotoolsTypes("4.1")
public operator fun AnyInt.plus(other: Int): Int = toInt() + other

/** Adds the [other] integer to this one. */
@SinceKotoolsTypes("4.1")
public operator fun AnyInt.plus(other: AnyInt): Int = toInt() + other

internal sealed interface AnyIntSerializer<I : AnyInt> : KSerializer<I> {
val serialName: Result<NotBlankString>

Expand Down
26 changes: 26 additions & 0 deletions src/commonTest/kotlin/kotools/types/number/AnyIntTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@ import kotools.types.shouldEqual
import kotlin.random.Random
import kotlin.test.Test

class AnyIntTest {
@Test
fun int_plus_should_pass() {
val x: Int = Random.nextInt()
val y: AnyInt = NonZeroInt.random()
val result: Int = x + y
result shouldEqual x + y.toInt()
}

@Test
fun plus_should_pass_with_an_Int() {
val x: AnyInt = PositiveInt.random()
val y: Int = Random.nextInt()
val result: Int = x + y
result shouldEqual x.toInt() + y
}

@Test
fun plus_should_pass_with_an_AnyInt() {
val x: AnyInt = PositiveInt.random()
val y: AnyInt = NegativeInt.random()
val result: Int = x + y
result shouldEqual x.toInt() + y.toInt()
}
}

class AnyIntSerializerTest {
private val serializer: KSerializer<AnyInt> = AnyIntSerializerImplementation

Expand Down

0 comments on commit 36af7a3

Please sign in to comment.