English 中文(简体)
在科特林, 是否有办法界定一般论点,如果它们不延伸普通基类(String/ByteArray)的话,这些论点只能是某些类型?
原标题:In Kotlin, Is there way to define generic arguments which can only be certain types when they don t extend a common base class (String/ByteArray)?

我有2项类似职能:

fun f1(String key, value: JsonNullable<ByteArray?>): Modification? {
    return if (value.isPresent) {
        when (val rawValue = value.get()) {
            null -> delete(key)
            else -> bar(field, rawValue)
        }
    } else {
        null
    }
}

================================================================================================================================================================================================================================================================

fun f2(String key, value: JsonNullable<String?>): Modification? {
    return if (value.isPresent) {
        when (val rawValue = value.get()) {
            null -> delete(key)
            else -> bar(field, rawValue)
        }
    } else {
        null
    }
}

是否有办法用单一功能加以取代?

bar function is overloaded:

fun bar(field: String, value: JsonNullable<ByteArray?>)...
fun bar(field: String, value: JsonNullable<String?>)...
问题回答

我认为,你想到的是,这种不正确的做法并不从拜特阿雷(或反之亦然)继承,除反对外,他们不继承任何基类。

Upper bounds The most common type of constraint is an upper bound, which corresponds to Java s extends keyword:

因此,除非你只是想成为任何内容。 目标(Bouting an ByteArray确实是扩展目标),需要使用通用物。

我认为,这个例子将使大家基本了解。

https://pl.kotl.in/umls8tYpV

你使用一种通用的表述,然后使用一种假设或改写的表述来做具体类型的事情。

如果使用上限,则将.se

因此,如果回答你的问题,你或许应该重新措辞,使之更确切地了解你所期待的东西。

Fair Appeal... Java/Scala 发育者很少见科特林,因此,某些 st子可能过于夸大。

同样,如果你愿意接受拜特阿雷,或设法使你能够这样做。

sealed class JsonType {
    data class JsonString(val value: String) : JsonType()
    data class JsonByteArray(val value: ByteArray) : JsonType()
}

data class JsonNullable<T>(val value: T?)

fun f2(key: String, value: JsonNullable<JsonType>?): String? {
    when (val jsonType = value?.value) {
        is JsonType.JsonString -> {
            println("Welp it is String: ${jsonType.value}")
        }
        is JsonType.JsonByteArray -> {
            println("Welp it is Byte: ${String(jsonType.value)}")
        }
        null -> {
            println("JSON is null")
        }
        else -> {
            println("Unknown JSON type")
        }
    }
    return null
}

/**
 * You can edit, run, and share this code.
 * play.kotlinlang.org
 */
fun main() {
    f2("null", null)
    f2("String", JsonNullable(JsonType.JsonString("String Value")))
    f2("Byte", JsonNullable(JsonType.JsonByteArray("Byte Value".toByteArray())))
    f2("Null JSON", JsonNullable(null))
}

. Kotlin关于通用s的文件,可在以下类型变量中撰写单一通用功能:

fun <T> f(String key, value: JsonNullable<T>): Modification? {
    return if (value.isPresent) {
        when (val rawValue = value.get()) {
            null -> delete(key)
            else -> bar(field, rawValue)
        }
    } else {
        null
    }
}




相关问题
Generic constraint exclusion

Sorry for asking silly question Is it possible to enforce constraint on generic in such a way that the given T can be derived from any reference Type except some A,B,C (where A,B,C are reference ...

Can a Type Derive from Itself?

When enforcing a Generic constraint class GenericTest { public void Sample<T>(T someValue) where T:Racer,new() { Console.WriteLine(someValue.Car); } } The Type T should be ...

C# Deriving Generic Methods

When i need to pass a Generic Type I can use the Syntax (Example : Ofcourse it is not a Generic Method) public void Sample(T someValue) { ...... } What is the benefit of declaring Sample<T>...

What is the benefit to using List<T> over IEnumerable<T>?

or the other way around? I use generic lists all the time. But I hear occasionally about IEnumerables, too, and I honestly have no clue (today) what they are for and why I should use them. So, at ...

热门标签