Skip to content
This repository was archived by the owner on Jul 8, 2022. It is now read-only.

Commit 0cf0081

Browse files
authored
Fix issue #168: Change inline classes to value classes (#171)
1 parent 1cc2e50 commit 0cf0081

File tree

20 files changed

+51
-25
lines changed

20 files changed

+51
-25
lines changed

klock/src/commonMain/kotlin/com/soywiz/klock/Date.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package com.soywiz.klock
22

33
import com.soywiz.klock.internal.Serializable
4+
import kotlin.jvm.JvmInline
45
import kotlin.math.abs
56

67
/**
78
* Represents a triple of [year], [month] and [day].
89
*
9-
* It is packed in an inline class wrapping an Int to prevent allocations.
10+
* It is packed in a value class wrapping an Int to prevent allocations.
1011
*/
11-
inline class Date(val encoded: Int) : Comparable<Date>, Serializable {
12+
@JvmInline
13+
value class Date(val encoded: Int) : Comparable<Date>, Serializable {
1214
companion object {
1315
@Suppress("MayBeConstant", "unused")
1416
private const val serialVersionUID = 1L

klock/src/commonMain/kotlin/com/soywiz/klock/DateTime.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.soywiz.klock
22

33
import com.soywiz.klock.internal.*
4+
import kotlin.jvm.JvmInline
45
import kotlin.math.*
56

67
/**
@@ -11,7 +12,8 @@ import kotlin.math.*
1112
* - Thu Aug 10 -140744 07:15:45 GMT-0014 (Central European Summer Time)
1213
* - Wed May 23 144683 18:29:30 GMT+0200 (Central European Summer Time)
1314
*/
14-
inline class DateTime(
15+
@JvmInline
16+
value class DateTime(
1517
/** Number of milliseconds since UNIX [EPOCH] */
1618
val unixMillis: Double
1719
) : Comparable<DateTime>, Serializable {

klock/src/commonMain/kotlin/com/soywiz/klock/Frequency.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.soywiz.klock
22

33
import com.soywiz.klock.hr.hr
4+
import kotlin.jvm.JvmInline
45

56
val TimeSpan.hz get() = timesPerSecond
67
val Int.hz get() = timesPerSecond
@@ -12,7 +13,8 @@ val TimeSpan.timesPerSecond get() = Frequency(1.0 / this.seconds)
1213
val Int.timesPerSecond get() = Frequency(this.toDouble())
1314
val Double.timesPerSecond get() = Frequency(this)
1415

15-
inline class Frequency(val hertz: Double) {
16+
@JvmInline
17+
value class Frequency(val hertz: Double) {
1618
companion object {
1719
fun from(timeSpan: TimeSpan) = timeSpan.toFrequency()
1820
}

klock/src/commonMain/kotlin/com/soywiz/klock/MonthSpan.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.soywiz.klock
22

33
import com.soywiz.klock.internal.Serializable
4+
import kotlin.jvm.JvmInline
45

56
/**
67
* Creates a [MonthSpan] representing these years.
@@ -15,7 +16,8 @@ inline val Int.months get() = MonthSpan(this)
1516
/**
1617
* Represents a number of years and months temporal distance.
1718
*/
18-
inline class MonthSpan(
19+
@JvmInline
20+
value class MonthSpan(
1921
/** Total months of this [MonthSpan] as integer */
2022
val totalMonths: Int
2123
) : Comparable<MonthSpan>, Serializable {

klock/src/commonMain/kotlin/com/soywiz/klock/NumberOfTimes.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.soywiz.klock
22

3+
import kotlin.jvm.JvmInline
4+
35
val infiniteTimes get() = NumberOfTimes.INFINITE
46
inline val Int.times get() = NumberOfTimes(this)
57

6-
inline class NumberOfTimes(val count: Int) {
8+
@JvmInline
9+
value class NumberOfTimes(val count: Int) {
710
companion object {
811
val ZERO = NumberOfTimes(0)
912
val ONE = NumberOfTimes(1)

klock/src/commonMain/kotlin/com/soywiz/klock/Time.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.soywiz.klock
22

33
import com.soywiz.klock.internal.Serializable
4+
import kotlin.jvm.JvmInline
45
import kotlin.math.abs
56

67
/**
78
* Represents a union of [millisecond], [second], [minute] and [hour].
89
*/
9-
inline class Time(val encoded: TimeSpan) : Comparable<Time>, Serializable {
10+
@JvmInline
11+
value class Time(val encoded: TimeSpan) : Comparable<Time>, Serializable {
1012
companion object {
1113
@Suppress("MayBeConstant", "unused")
1214
private const val serialVersionUID = 1L

klock/src/commonMain/kotlin/com/soywiz/klock/TimeSpan.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.soywiz.klock
22

33
import com.soywiz.klock.internal.*
4+
import kotlin.jvm.JvmInline
45
import kotlin.math.*
56

67
/** [TimeSpan] representing this number as [nanoseconds] or 1 / 1_000_000_000 [seconds]. */
@@ -75,9 +76,10 @@ inline val Double.weeks get() = TimeSpan.fromWeeks(this)
7576
/**
7677
* Represents a span of time, with [milliseconds] precision.
7778
*
78-
* It is an inline class wrapping [Double] instead of [Long] to work on JavaScript without allocations.
79+
* It is a value class wrapping [Double] instead of [Long] to work on JavaScript without allocations.
7980
*/
80-
inline class TimeSpan(
81+
@JvmInline
82+
value class TimeSpan(
8183
/** Returns the total number of [milliseconds] for this [TimeSpan] (1 / 1_000 [seconds]) */
8284
val milliseconds: Double
8385
) : Comparable<TimeSpan>, Serializable {

klock/src/commonMain/kotlin/com/soywiz/klock/TimezoneOffset.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.soywiz.klock
22

33
import com.soywiz.klock.internal.*
4+
import kotlin.jvm.JvmInline
45
import kotlin.math.*
56

67
/**
@@ -9,7 +10,8 @@ import kotlin.math.*
910
*
1011
* This class is inlined so no boxing should be required.
1112
*/
12-
inline class TimezoneOffset(
13+
@JvmInline
14+
value class TimezoneOffset(
1315
/** [TimezoneOffset] in [totalMilliseconds] */
1416
val totalMilliseconds: Double
1517
) : Comparable<TimezoneOffset>, Serializable {

klock/src/commonMain/kotlin/com/soywiz/klock/Year.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.soywiz.klock
22

33
import com.soywiz.klock.internal.Serializable
4+
import kotlin.jvm.JvmInline
45
import kotlin.math.*
56

67
/**
@@ -11,7 +12,8 @@ import kotlin.math.*
1112
*
1213
* The integrated model is capable of determine if a year is leap for years 1 until 9999 inclusive.
1314
*/
14-
inline class Year(val year: Int) : Comparable<Year>, Serializable {
15+
@JvmInline
16+
value class Year(val year: Int) : Comparable<Year>, Serializable {
1517
companion object {
1618
@Suppress("MayBeConstant", "unused")
1719
private const val serialVersionUID = 1L

klock/src/commonMain/kotlin/com/soywiz/klock/YearMonth.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.soywiz.klock
22

33
import com.soywiz.klock.internal.Serializable
4+
import kotlin.jvm.JvmInline
45

56
/**
67
* Represents a couple of [year] and [month].
78
*
8-
* It is packed in an inline class wrapping an Int to prevent allocations.
9+
* It is packed in a value class wrapping an Int to prevent allocations.
910
*/
10-
inline class YearMonth(internal val internalPackedInfo: Int) : Serializable {
11+
@JvmInline
12+
value class YearMonth(internal val internalPackedInfo: Int) : Serializable {
1113
companion object {
1214
@Suppress("MayBeConstant", "unused")
1315
private const val serialVersionUID = 1L

0 commit comments

Comments
 (0)