Kotlin Infix功能带有仿制药

发布于 2025-02-02 04:35:22 字数 134 浏览 3 评论 0原文

尝试编写一个简单的Kotlin Infix函数以进行加操作。通用类型怎么了?

infix fun <T : Number> T.myPlus(that: T): T = this + that

Try to write a simple Kotlin infix function for plus operation. What's wrong with generic type?

infix fun <T : Number> T.myPlus(that: T): T = this + that

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怂人 2025-02-09 04:35:22

正如其他人提到的那样,由于各种原因,没有使用仿制药的解决方案。您必须为每种数字类型(字节,短,int,long,float,double)定义一个扩展功能。例如,您可以这样做:

when (that) {
    is Byte, is Short, is Int, is Long -> that.toLong().plus(this)
    is Float -> that + this
    is Double -> that + this
    else -> throw Exception("Types mismatch")
}

即使这样做,在某些情况下,您还需要决定是否要截断或围绕结果。

val n1 = 1230000000000000000L
val n2 = 123.7

这种情况(n1是长的,n2是浮点)可以像这样处理:

is Float -> this + that.toLong()

导致12330000000000000123

,或者可以这样处理:

is Float -> this.toFloat() + that

导致1.23E18中。

As others have mentioned, there's no solutions using generics for various reasons. You have to define an extension function for each Number type (Byte, Short, Int, Long, Float, Double). E.g. for Int you could do:

when (that) {
    is Byte, is Short, is Int, is Long -> that.toLong().plus(this)
    is Float -> that + this
    is Double -> that + this
    else -> throw Exception("Types mismatch")
}

Even doing that, in some cases you need to decide whether you want to truncate or round the result.

val n1 = 1230000000000000000L
val n2 = 123.7

This case (n1 is a Long, n2 is a Float) could be handled like this:

is Float -> this + that.toLong()

resulting in 1230000000000000123

or it could be handled like this:

is Float -> this.toFloat() + that

resulting in 1.23E18

怎樣才叫好 2025-02-09 04:35:22

您忘记了运算符关键字:


infix运算符fun t.plus(that:t):t = t = this + tht that

edit:

infix fun Number.infixPlus(that: Number): Number =
  when (that) {
    is Int    -> this.toInt() + that
    is Long   -> this.toLong() + that
    is Float  -> this.toFloat() + that
    is Double -> this.toDouble() + that
    else      -> throw Exception()
  }

val n1 = 123
val n2 = 123.456

val result = n1 infixPlus n2

println("result: " + result)

println("is Int: " + (result is Int))
println("is Long: " + (result is Long))
println("is Float: " + (result is Float))
println("is Double: " + (result is Double))

You forgot the operator keyword:

infix operator fun T.plus(that: T): T = this + that

Edit:

infix fun Number.infixPlus(that: Number): Number =
  when (that) {
    is Int    -> this.toInt() + that
    is Long   -> this.toLong() + that
    is Float  -> this.toFloat() + that
    is Double -> this.toDouble() + that
    else      -> throw Exception()
  }

val n1 = 123
val n2 = 123.456

val result = n1 infixPlus n2

println("result: " + result)

println("is Int: " + (result is Int))
println("is Long: " + (result is Long))
println("is Float: " + (result is Float))
println("is Double: " + (result is Double))
固执像三岁 2025-02-09 04:35:22

更多的铸造安全性:

infix fun Number.infixPlus(that: Number): Number {
    return when {
        this is Int && that is Int -> this + that
        this is Double && that is Double -> this + that
        this is Float && that is Float -> this + that
        else -> throw Exception("Types mismatch")
    }
}

上述。示例args:

val n1 = 1230000000000000000L
val n2 = 123

val result = n1 infixPlus n2
result: -1313144709

没有例外和错误结果。

A bit more casting safety:

infix fun Number.infixPlus(that: Number): Number {
    return when {
        this is Int && that is Int -> this + that
        this is Double && that is Double -> this + that
        this is Float && that is Float -> this + that
        else -> throw Exception("Types mismatch")
    }
}

In prev. example args:

val n1 = 1230000000000000000L
val n2 = 123

val result = n1 infixPlus n2
result: -1313144709

No exception and wrong result.

薄荷港 2025-02-09 04:35:22

您必须找到结果的正确类型:

val types = listOf("Double", "Float", "Long", "Integer", "Short", "Byte")
infix fun <T : Number> T.myPlus(that: T): T {
    return when(types[min(types.indexOf(this.javaClass.simpleName), types.indexOf(that.javaClass.simpleName))]) {
        types[0] -> (this.toDouble() + that.toDouble()) as T
        types[1] -> (this.toFloat() + that.toFloat()) as T
        types[2] -> (this.toLong() + that.toLong()) as T
        types[3] -> (this.toInt() + that.toInt()) as T
        types[4] -> (this.toShort() + that.toShort()) as T
        types[5] -> (this.toByte() + that.toByte()) as T
        else -> throw IllegalArgumentException()
    }
}

You have to find the correct type of the result:

val types = listOf("Double", "Float", "Long", "Integer", "Short", "Byte")
infix fun <T : Number> T.myPlus(that: T): T {
    return when(types[min(types.indexOf(this.javaClass.simpleName), types.indexOf(that.javaClass.simpleName))]) {
        types[0] -> (this.toDouble() + that.toDouble()) as T
        types[1] -> (this.toFloat() + that.toFloat()) as T
        types[2] -> (this.toLong() + that.toLong()) as T
        types[3] -> (this.toInt() + that.toInt()) as T
        types[4] -> (this.toShort() + that.toShort()) as T
        types[5] -> (this.toByte() + that.toByte()) as T
        else -> throw IllegalArgumentException()
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文