From 7a533d4667dd0a0b5babf1debf7e2f37c9537bf0 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 20 Sep 2020 13:56:12 +0800 Subject: [PATCH] Use lazy for SemVersion.toString --- .../mamoe/mirai/console/util/SemVersion.kt | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/SemVersion.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/SemVersion.kt index f07b8d5b9..939fe7ea6 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/SemVersion.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/SemVersion.kt @@ -188,23 +188,22 @@ public data class SemVersion internal constructor( } @Transient - private var toString: String? = null // For cache. - override fun toString(): String { - return toString ?: kotlin.run { - buildString { - mainVersion.joinTo(this, ".") - identifier?.let { identifier -> - append('-') - append(identifier) - } - metadata?.let { metadata -> - append('+') - append(metadata) - } - }.also { toString = it } + private val toString: String by lazy(LazyThreadSafetyMode.NONE) { + buildString { + mainVersion.joinTo(this, ".") + identifier?.let { identifier -> + append('-') + append(identifier) + } + metadata?.let { metadata -> + append('+') + append(metadata) + } } } + override fun toString(): String = toString + /** * 将 [SemVersion] 转为 Kotlin data class 风格的 [String] */