Generics 合并返回空集合和空列表的方法

Generics 合并返回空集合和空列表的方法,generics,kotlin,Generics,Kotlin,这两个功能非常相似 fun <T> emptyList(): List<T> = emptyList<T>() fun <T> emptySet(): Set<T> = emptySet<T>() fun-emptyList():List=emptyList() fun emptySet():Set=emptySet() 是否有某种方法可以将它们组合成一个返回空集或空列表的方法?如果调用方必须传递要返回的空集合,那就可

这两个功能非常相似

fun <T> emptyList(): List<T> = emptyList<T>()

fun <T> emptySet(): Set<T> = emptySet<T>()
fun-emptyList():List=emptyList()
fun emptySet():Set=emptySet()

是否有某种方法可以将它们组合成一个返回空集或空列表的方法?如果调用方必须传递要返回的空集合,那就可以了


理想情况下,我希望在调用方不需要强制转换的情况下实现这一点,即使用
as
is
不可能同时返回两种类型。你必须以某种方式决定它

列表
集合
您可以创建从现有集合创建空集合的集合

fun <T> List<T>.empty() = emptyList<T>()
fun <T> Set<T>.empty() = emptySet<T>()
您可以使用已定义的函数和

val-emptyList:List=empty().toList()

您可以创建一个界面,该界面同时实现
列表
设置

接口列表集:列表,设置{
//必须覆盖此乐趣,因为它在'List'和'Set'中定义`
重写有趣的拆分器():拆分器
}
现在,您可以从函数返回其实例:

private object EmptyListSet :
    ListSet<Nothing>,
    List<Nothing> by emptyList(),
    Set<Nothing> by emptySet() {
    override fun spliterator(): Spliterator<Nothing> = emptyList<Nothing>().spliterator()
    // Must override following elements because they can be delegated to `List` and `Set`
    override val size: Int get() = 0
    override fun contains(element: Nothing) = false
    override fun containsAll(elements: Collection<Nothing>) = false
    override fun isEmpty() = true
    override fun iterator() = emptyList<Nothing>().iterator()
}

fun <E> emptyListSet(): ListSet<E> = EmptyListSet
私有对象EmptyList集:
列表集,
按空列表()列出,
由emptySet()设置{
重写有趣的拆分器():拆分器=空列表()。拆分器()
//必须重写以下元素,因为它们可以委托给'List'和'Set'`
覆盖值大小:Int get()=0
覆盖包含(元素:Nothing)=false
覆盖所有元素(元素:集合)=false
override fun isEmpty()=true
重写有趣的迭代器()=emptyList()。迭代器()
}
fun emptyListSet():ListSet=emptyListSet
用例:

val list: List<Int> = emptyListSet()
val set: Set<String> = emptyListSet()
val list:list=emptyListSet()
val set:set=emptyListSet()

可能是这样的:

import kotlin.reflect.KClass

fun main() {
    val emptyList = emptyCollection(List::class)
    val emptySet = emptyCollection(Set::class)
}

fun <T: Collection<*>> emptyCollection(clazz: KClass<T>): Collection<Nothing> {
    return when (clazz) {
        List::class -> emptyList<Nothing>()
        Set::class -> emptySet<Nothing>()
        else -> throw IllegalArgumentException()
    }
}
import kotlin.reflect.KClass
主要内容(){
val emptyList=emptyCollection(列表::类)
val emptySet=emptyCollection(集合::类)
}
有趣的空集合(类别:K类):集合{
返回时间(clazz){
列表::类->空列表()
Set::class->emptySet()
else->抛出IllegalArgumentException()
}
}

“如果调用者必须传递要返回的空集合,那就好了。”如果他们手头上已经有这个集合,为什么需要一个返回它的方法<在这种情况下,code>fun(x:T)=x会起作用,但这可能不是你想要的。
fun empty():Collection=empty()
@QingfeiYuan我想你的意思是
emptyList
emptySet
interface ListSet<out E> : List<E>, Set<E> {
    // Must override this fun because it's defined in `List` and `Set`
    override fun spliterator(): Spliterator<@UnsafeVariance E>
}
private object EmptyListSet :
    ListSet<Nothing>,
    List<Nothing> by emptyList(),
    Set<Nothing> by emptySet() {
    override fun spliterator(): Spliterator<Nothing> = emptyList<Nothing>().spliterator()
    // Must override following elements because they can be delegated to `List` and `Set`
    override val size: Int get() = 0
    override fun contains(element: Nothing) = false
    override fun containsAll(elements: Collection<Nothing>) = false
    override fun isEmpty() = true
    override fun iterator() = emptyList<Nothing>().iterator()
}

fun <E> emptyListSet(): ListSet<E> = EmptyListSet
val list: List<Int> = emptyListSet()
val set: Set<String> = emptyListSet()
import kotlin.reflect.KClass

fun main() {
    val emptyList = emptyCollection(List::class)
    val emptySet = emptyCollection(Set::class)
}

fun <T: Collection<*>> emptyCollection(clazz: KClass<T>): Collection<Nothing> {
    return when (clazz) {
        List::class -> emptyList<Nothing>()
        Set::class -> emptySet<Nothing>()
        else -> throw IllegalArgumentException()
    }
}