From cd8f21d0732f2702d87964be5d991a4b9903bd99 Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 10 Sep 2020 18:34:01 +0800 Subject: [PATCH] Rename `scopeWithNotNull` to `scopeWith` --- .../console/codegen/MessageScopeCodegen.kt | 76 +++---- .../mamoe/mirai/console/util/MessageScope.kt | 189 ++++-------------- 2 files changed, 74 insertions(+), 191 deletions(-) diff --git a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/MessageScopeCodegen.kt b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/MessageScopeCodegen.kt index 0f4cf60b2..69b39e2e3 100644 --- a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/MessageScopeCodegen.kt +++ b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/MessageScopeCodegen.kt @@ -84,46 +84,46 @@ internal object MessageScopeCodegen { ) appendLine() } +// +// for ((a, b) in (TypeCandidatesForMessageScope + KtMessageScope).arrangements()) { +// appendKCode( +// """ +// @LowPriorityInOverloadResolution +// public fun ${a}.scopeWith(vararg others: ${b}): MessageScope { +// return others.fold(this.asMessageScope()) { acc, other -> CombinedScope(acc, other.asMessageScope()) } +// } +// """ +// ) +// appendLine() +// } for ((a, b) in (TypeCandidatesForMessageScope + KtMessageScope).arrangements()) { appendKCode( """ @LowPriorityInOverloadResolution - public fun ${a}.scopeWith(vararg others: ${b}): MessageScope { - return others.fold(this.asMessageScope()) { acc, other -> CombinedScope(acc, other.asMessageScope()) } + public fun ${a}?.scopeWith(vararg others: ${b}?): MessageScope { + return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWith(other?.asMessageScope()) } } """ ) appendLine() } +// +// for ((a, b) in (TypeCandidatesForMessageScope + KtMessageScope).arrangements()) { +// appendKCode( +// """ +// public fun ${a}.scopeWith(other: ${b}): MessageScope { +// return CombinedScope(asMessageScope(), other.asMessageScope()) +// } +// """ +// ) +// appendLine() +// } for ((a, b) in (TypeCandidatesForMessageScope + KtMessageScope).arrangements()) { appendKCode( """ - @LowPriorityInOverloadResolution - public fun ${a}?.scopeWithNotNull(vararg others: ${b}?): MessageScope { - return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWithNotNull(other?.asMessageScope()) } - } - """ - ) - appendLine() - } - - for ((a, b) in (TypeCandidatesForMessageScope + KtMessageScope).arrangements()) { - appendKCode( - """ - public fun ${a}.scopeWith(other: ${b}): MessageScope { - return CombinedScope(asMessageScope(), other.asMessageScope()) - } - """ - ) - appendLine() - } - - for ((a, b) in (TypeCandidatesForMessageScope + KtMessageScope).arrangements()) { - appendKCode( - """ - public fun ${a}?.scopeWithNotNull(other: ${b}?): MessageScope { + public fun ${a}?.scopeWith(other: ${b}?): MessageScope { @Suppress("DuplicatedCode") return when { this == null && other == null -> NoopMessageScope @@ -137,11 +137,22 @@ internal object MessageScopeCodegen { ) appendLine() } +// +// for ((a, b) in (TypeCandidatesForMessageScope + KtMessageScope).arrangements()) { +// appendKCode( +// """ +// public inline fun ${a}.scopeWith(vararg others: ${b}, action: MessageScope.() -> R): R { +// return scopeWith(*others).invoke(action) +// } +// """ +// ) +// appendLine() +// } for ((a, b) in (TypeCandidatesForMessageScope + KtMessageScope).arrangements()) { appendKCode( """ - public inline fun ${a}.scopeWith(vararg others: ${b}, action: MessageScope.() -> R): R { + public inline fun ${a}?.scopeWith(vararg others: ${b}?, action: MessageScope.() -> R): R { return scopeWith(*others).invoke(action) } """ @@ -149,17 +160,6 @@ internal object MessageScopeCodegen { appendLine() } - for ((a, b) in (TypeCandidatesForMessageScope + KtMessageScope).arrangements()) { - appendKCode( - """ - public inline fun ${a}?.scopeWithNotNull(vararg others: ${b}?, action: MessageScope.() -> R): R { - return scopeWithNotNull(*others).invoke(action) - } - """ - ) - appendLine() - } - for (a in (TypeCandidatesForMessageScope + KtMessageScope)) { appendKCode( """ diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/MessageScope.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/MessageScope.kt index a4ad3e1e1..e8f46c220 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/MessageScope.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/MessageScope.kt @@ -142,132 +142,51 @@ public fun Contact.asMessageScope(): MessageScope = createScopeDelegate(this) public fun CommandSender.asMessageScope(): MessageScope = createScopeDelegate(this) @LowPriorityInOverloadResolution -public fun Contact.scopeWith(vararg others: Contact): MessageScope { - return others.fold(this.asMessageScope()) { acc, other -> CombinedScope(acc, other.asMessageScope()) } +public fun Contact?.scopeWith(vararg others: Contact?): MessageScope { + return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWith(other?.asMessageScope()) } } @LowPriorityInOverloadResolution -public fun Contact.scopeWith(vararg others: CommandSender): MessageScope { - return others.fold(this.asMessageScope()) { acc, other -> CombinedScope(acc, other.asMessageScope()) } +public fun Contact?.scopeWith(vararg others: CommandSender?): MessageScope { + return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWith(other?.asMessageScope()) } } @LowPriorityInOverloadResolution -public fun Contact.scopeWith(vararg others: MessageScope): MessageScope { - return others.fold(this.asMessageScope()) { acc, other -> CombinedScope(acc, other.asMessageScope()) } +public fun Contact?.scopeWith(vararg others: MessageScope?): MessageScope { + return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWith(other?.asMessageScope()) } } @LowPriorityInOverloadResolution -public fun CommandSender.scopeWith(vararg others: Contact): MessageScope { - return others.fold(this.asMessageScope()) { acc, other -> CombinedScope(acc, other.asMessageScope()) } +public fun CommandSender?.scopeWith(vararg others: Contact?): MessageScope { + return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWith(other?.asMessageScope()) } } @LowPriorityInOverloadResolution -public fun CommandSender.scopeWith(vararg others: CommandSender): MessageScope { - return others.fold(this.asMessageScope()) { acc, other -> CombinedScope(acc, other.asMessageScope()) } +public fun CommandSender?.scopeWith(vararg others: CommandSender?): MessageScope { + return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWith(other?.asMessageScope()) } } @LowPriorityInOverloadResolution -public fun CommandSender.scopeWith(vararg others: MessageScope): MessageScope { - return others.fold(this.asMessageScope()) { acc, other -> CombinedScope(acc, other.asMessageScope()) } +public fun CommandSender?.scopeWith(vararg others: MessageScope?): MessageScope { + return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWith(other?.asMessageScope()) } } @LowPriorityInOverloadResolution -public fun MessageScope.scopeWith(vararg others: Contact): MessageScope { - return others.fold(this.asMessageScope()) { acc, other -> CombinedScope(acc, other.asMessageScope()) } +public fun MessageScope?.scopeWith(vararg others: Contact?): MessageScope { + return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWith(other?.asMessageScope()) } } @LowPriorityInOverloadResolution -public fun MessageScope.scopeWith(vararg others: CommandSender): MessageScope { - return others.fold(this.asMessageScope()) { acc, other -> CombinedScope(acc, other.asMessageScope()) } +public fun MessageScope?.scopeWith(vararg others: CommandSender?): MessageScope { + return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWith(other?.asMessageScope()) } } @LowPriorityInOverloadResolution -public fun MessageScope.scopeWith(vararg others: MessageScope): MessageScope { - return others.fold(this.asMessageScope()) { acc, other -> CombinedScope(acc, other.asMessageScope()) } +public fun MessageScope?.scopeWith(vararg others: MessageScope?): MessageScope { + return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWith(other?.asMessageScope()) } } -@LowPriorityInOverloadResolution -public fun Contact?.scopeWithNotNull(vararg others: Contact?): MessageScope { - return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWithNotNull(other?.asMessageScope()) } -} - -@LowPriorityInOverloadResolution -public fun Contact?.scopeWithNotNull(vararg others: CommandSender?): MessageScope { - return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWithNotNull(other?.asMessageScope()) } -} - -@LowPriorityInOverloadResolution -public fun Contact?.scopeWithNotNull(vararg others: MessageScope?): MessageScope { - return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWithNotNull(other?.asMessageScope()) } -} - -@LowPriorityInOverloadResolution -public fun CommandSender?.scopeWithNotNull(vararg others: Contact?): MessageScope { - return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWithNotNull(other?.asMessageScope()) } -} - -@LowPriorityInOverloadResolution -public fun CommandSender?.scopeWithNotNull(vararg others: CommandSender?): MessageScope { - return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWithNotNull(other?.asMessageScope()) } -} - -@LowPriorityInOverloadResolution -public fun CommandSender?.scopeWithNotNull(vararg others: MessageScope?): MessageScope { - return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWithNotNull(other?.asMessageScope()) } -} - -@LowPriorityInOverloadResolution -public fun MessageScope?.scopeWithNotNull(vararg others: Contact?): MessageScope { - return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWithNotNull(other?.asMessageScope()) } -} - -@LowPriorityInOverloadResolution -public fun MessageScope?.scopeWithNotNull(vararg others: CommandSender?): MessageScope { - return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWithNotNull(other?.asMessageScope()) } -} - -@LowPriorityInOverloadResolution -public fun MessageScope?.scopeWithNotNull(vararg others: MessageScope?): MessageScope { - return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWithNotNull(other?.asMessageScope()) } -} - -public fun Contact.scopeWith(other: Contact): MessageScope { - return CombinedScope(asMessageScope(), other.asMessageScope()) -} - -public fun Contact.scopeWith(other: CommandSender): MessageScope { - return CombinedScope(asMessageScope(), other.asMessageScope()) -} - -public fun Contact.scopeWith(other: MessageScope): MessageScope { - return CombinedScope(asMessageScope(), other.asMessageScope()) -} - -public fun CommandSender.scopeWith(other: Contact): MessageScope { - return CombinedScope(asMessageScope(), other.asMessageScope()) -} - -public fun CommandSender.scopeWith(other: CommandSender): MessageScope { - return CombinedScope(asMessageScope(), other.asMessageScope()) -} - -public fun CommandSender.scopeWith(other: MessageScope): MessageScope { - return CombinedScope(asMessageScope(), other.asMessageScope()) -} - -public fun MessageScope.scopeWith(other: Contact): MessageScope { - return CombinedScope(asMessageScope(), other.asMessageScope()) -} - -public fun MessageScope.scopeWith(other: CommandSender): MessageScope { - return CombinedScope(asMessageScope(), other.asMessageScope()) -} - -public fun MessageScope.scopeWith(other: MessageScope): MessageScope { - return CombinedScope(asMessageScope(), other.asMessageScope()) -} - -public fun Contact?.scopeWithNotNull(other: Contact?): MessageScope { +public fun Contact?.scopeWith(other: Contact?): MessageScope { @Suppress("DuplicatedCode") return when { this == null && other == null -> NoopMessageScope @@ -278,7 +197,7 @@ public fun Contact?.scopeWithNotNull(other: Contact?): MessageScope { } } -public fun Contact?.scopeWithNotNull(other: CommandSender?): MessageScope { +public fun Contact?.scopeWith(other: CommandSender?): MessageScope { @Suppress("DuplicatedCode") return when { this == null && other == null -> NoopMessageScope @@ -289,7 +208,7 @@ public fun Contact?.scopeWithNotNull(other: CommandSender?): MessageScope { } } -public fun Contact?.scopeWithNotNull(other: MessageScope?): MessageScope { +public fun Contact?.scopeWith(other: MessageScope?): MessageScope { @Suppress("DuplicatedCode") return when { this == null && other == null -> NoopMessageScope @@ -300,7 +219,7 @@ public fun Contact?.scopeWithNotNull(other: MessageScope?): MessageScope { } } -public fun CommandSender?.scopeWithNotNull(other: Contact?): MessageScope { +public fun CommandSender?.scopeWith(other: Contact?): MessageScope { @Suppress("DuplicatedCode") return when { this == null && other == null -> NoopMessageScope @@ -311,7 +230,7 @@ public fun CommandSender?.scopeWithNotNull(other: Contact?): MessageScope { } } -public fun CommandSender?.scopeWithNotNull(other: CommandSender?): MessageScope { +public fun CommandSender?.scopeWith(other: CommandSender?): MessageScope { @Suppress("DuplicatedCode") return when { this == null && other == null -> NoopMessageScope @@ -322,7 +241,7 @@ public fun CommandSender?.scopeWithNotNull(other: CommandSender?): MessageScope } } -public fun CommandSender?.scopeWithNotNull(other: MessageScope?): MessageScope { +public fun CommandSender?.scopeWith(other: MessageScope?): MessageScope { @Suppress("DuplicatedCode") return when { this == null && other == null -> NoopMessageScope @@ -333,7 +252,7 @@ public fun CommandSender?.scopeWithNotNull(other: MessageScope?): MessageScope { } } -public fun MessageScope?.scopeWithNotNull(other: Contact?): MessageScope { +public fun MessageScope?.scopeWith(other: Contact?): MessageScope { @Suppress("DuplicatedCode") return when { this == null && other == null -> NoopMessageScope @@ -344,7 +263,7 @@ public fun MessageScope?.scopeWithNotNull(other: Contact?): MessageScope { } } -public fun MessageScope?.scopeWithNotNull(other: CommandSender?): MessageScope { +public fun MessageScope?.scopeWith(other: CommandSender?): MessageScope { @Suppress("DuplicatedCode") return when { this == null && other == null -> NoopMessageScope @@ -355,7 +274,7 @@ public fun MessageScope?.scopeWithNotNull(other: CommandSender?): MessageScope { } } -public fun MessageScope?.scopeWithNotNull(other: MessageScope?): MessageScope { +public fun MessageScope?.scopeWith(other: MessageScope?): MessageScope { @Suppress("DuplicatedCode") return when { this == null && other == null -> NoopMessageScope @@ -366,78 +285,42 @@ public fun MessageScope?.scopeWithNotNull(other: MessageScope?): MessageScope { } } -public inline fun Contact.scopeWith(vararg others: Contact, action: MessageScope.() -> R): R { +public inline fun Contact?.scopeWith(vararg others: Contact?, action: MessageScope.() -> R): R { return scopeWith(*others).invoke(action) } -public inline fun Contact.scopeWith(vararg others: CommandSender, action: MessageScope.() -> R): R { +public inline fun Contact?.scopeWith(vararg others: CommandSender?, action: MessageScope.() -> R): R { return scopeWith(*others).invoke(action) } -public inline fun Contact.scopeWith(vararg others: MessageScope, action: MessageScope.() -> R): R { +public inline fun Contact?.scopeWith(vararg others: MessageScope?, action: MessageScope.() -> R): R { return scopeWith(*others).invoke(action) } -public inline fun CommandSender.scopeWith(vararg others: Contact, action: MessageScope.() -> R): R { +public inline fun CommandSender?.scopeWith(vararg others: Contact?, action: MessageScope.() -> R): R { return scopeWith(*others).invoke(action) } -public inline fun CommandSender.scopeWith(vararg others: CommandSender, action: MessageScope.() -> R): R { +public inline fun CommandSender?.scopeWith(vararg others: CommandSender?, action: MessageScope.() -> R): R { return scopeWith(*others).invoke(action) } -public inline fun CommandSender.scopeWith(vararg others: MessageScope, action: MessageScope.() -> R): R { +public inline fun CommandSender?.scopeWith(vararg others: MessageScope?, action: MessageScope.() -> R): R { return scopeWith(*others).invoke(action) } -public inline fun MessageScope.scopeWith(vararg others: Contact, action: MessageScope.() -> R): R { +public inline fun MessageScope?.scopeWith(vararg others: Contact?, action: MessageScope.() -> R): R { return scopeWith(*others).invoke(action) } -public inline fun MessageScope.scopeWith(vararg others: CommandSender, action: MessageScope.() -> R): R { +public inline fun MessageScope?.scopeWith(vararg others: CommandSender?, action: MessageScope.() -> R): R { return scopeWith(*others).invoke(action) } -public inline fun MessageScope.scopeWith(vararg others: MessageScope, action: MessageScope.() -> R): R { +public inline fun MessageScope?.scopeWith(vararg others: MessageScope?, action: MessageScope.() -> R): R { return scopeWith(*others).invoke(action) } -public inline fun Contact?.scopeWithNotNull(vararg others: Contact?, action: MessageScope.() -> R): R { - return scopeWithNotNull(*others).invoke(action) -} - -public inline fun Contact?.scopeWithNotNull(vararg others: CommandSender?, action: MessageScope.() -> R): R { - return scopeWithNotNull(*others).invoke(action) -} - -public inline fun Contact?.scopeWithNotNull(vararg others: MessageScope?, action: MessageScope.() -> R): R { - return scopeWithNotNull(*others).invoke(action) -} - -public inline fun CommandSender?.scopeWithNotNull(vararg others: Contact?, action: MessageScope.() -> R): R { - return scopeWithNotNull(*others).invoke(action) -} - -public inline fun CommandSender?.scopeWithNotNull(vararg others: CommandSender?, action: MessageScope.() -> R): R { - return scopeWithNotNull(*others).invoke(action) -} - -public inline fun CommandSender?.scopeWithNotNull(vararg others: MessageScope?, action: MessageScope.() -> R): R { - return scopeWithNotNull(*others).invoke(action) -} - -public inline fun MessageScope?.scopeWithNotNull(vararg others: Contact?, action: MessageScope.() -> R): R { - return scopeWithNotNull(*others).invoke(action) -} - -public inline fun MessageScope?.scopeWithNotNull(vararg others: CommandSender?, action: MessageScope.() -> R): R { - return scopeWithNotNull(*others).invoke(action) -} - -public inline fun MessageScope?.scopeWithNotNull(vararg others: MessageScope?, action: MessageScope.() -> R): R { - return scopeWithNotNull(*others).invoke(action) -} - @Deprecated( "Senseless scopeWith. Use asMessageScope.", ReplaceWith("this.asMessageScope()", "net.mamoe.mirai.console.util.asMessageScope")