Tag: 操作符重载

未解决的时间运营商

我有以下代码 : val temp1 = Quat(1f, Vec3(0, 1, 0)).normalize() val temp2 = Quat(.5f, Vec3(1, 0, 0)).normalize() val a = temp1 * Vec3(0, 1, 0) val transformed0 = a * temp1.inverse() // error 使用此运算符 ,第一个*在Quat类中正确解析: operator fun times(b: Vec3) = mul(Vec3(), this, b) 所以a是Vec3 ,最后一行我们得到了Vec3 * Quat 我想使用的第二个运算符是在此文件的顶层定义的: operator fun Vec3.times(b: Quat) = mul(Vec3(), this, b) […]

Java方法错误地自动在kotlin中重载

给定一个包含以下(精简)类的Java库: public class Vector2f { public float x; public float y; public Vector2f div(Vector2f other) { x /= other.x; y /= other.y; return this; } public Vector2f div(Vector2f other, Vector2f dest) { dest.x = x / other.x; dest.y = y / other.y; return dest; } /* … */ } 由于kotlin自动将合适的方法名称转换为重载操作符,我可以写 val v0 = Vector2f(12f, 12f) […]

如何添加运算符扩展作为特定类的上下文的一部分而无需子类化?

我试图利用运营商的Wicket,这是痛苦的详细。 我最想要的功能是使用一个“+”来add()一个组件。 但是需要在每个MarkupContainer后代的内部工作。 使用应该是这样的: class SomePage() : WebPage() { init { // SomePage{} context +Label("someLabel","Some label") // instead of this.add(Label("someLabel","Some label")) +object : StatelessForm<Unit>("someForm") { init { // StatelessForm{} context +Label("fieldLabel","Field label") +RequiredTextField("someField") } } } } 现在是否可以实现这一点,而不需要继承所有东西? 我想要的一些想象的语法: extend org.apache.wicket.MarkupContainer { operator fun<T: Component> T.unaryPlus():T { // add() is called as a method of […]

为“Number Classes”重载+和+ =运算符

我想为封装简单Number的类创建扩展函数。 例如DoubleProperty 。 我遇到了这个问题,我不能同时重载+和+=运算符。 我不想创建一个行为,通过以下测试: class DoublePropertyTest { lateinit var doubleProperty: DoubleProperty @Before fun initialize() { doubleProperty = SimpleDoubleProperty(0.1) } @Test fun plus() { val someProperty = doubleProperty + 1.5 assertEquals(someProperty.value, 1.6, 0.001) } @Test fun plusAssign() { val someProperty = doubleProperty doubleProperty += 1.5 //error if + and += are overloaded assert(someProperty === doubleProperty) […]

在Kotlin中调用操作符&运算符重载

我开始了解Invoke操作符, a()相当于a.invoke() 有什么更多关于调用操作员,然后请解释。 此外,我没有得到任何Invoke运算符重载的例子。 调用操作符是否可以重载? 如果可能的话,任何人都可以请示例解释有关Invoke操作符重载。 我没有得到任何关于这个。 提前致谢。

定义equals()运算符的问题

我有一堂课 open class Texture 我想定义equals(other: Texture)运算符 operator fun equals(other: Texture) = … 但是我明白了 错误:(129,5)Kotlin:“运算符”修饰符不适用于此函数:必须覆盖Any中的“equals()” 这是什么意思? 如果我改变了 operator fun equals(other: Any) = … 意外覆盖,两个声明具有相同的jvm签名

Kotlin:编译器不会推断一些运算符的泛型类型

为什么Kotlin的编译器不能用一些运算符调用来推断泛型? 例如: class Example { operator inline fun <reified T : Any> unaryMinus(): T { … } } 在主要方法中使用它… fun main(args: Array<String>) { val ex = Example() val works: Boolean = ex.unaryMinus() val doesntWork: Boolean = -ex }

可以使用Kotlin中的比较运算符重载来实现类似于SQL的功能吗?

Kotlin有限的运算符超载。 我正在包装一个基于矢量的操作的API,因为它们是用R,Julia,APL等语言完成的。最常见的例子是在SQL中,你可以说: select * from foo where bar > 3; 使得每个记录都被单独评估,并且如果通过比较则被添加到结果集。 在Kotlin中,比较运算符重载与compareTo方法相关联: Expression Translated to a > b a.compareTo(b) > 0 a < b a.compareTo(b) < 0 a >= b a.compareTo(b) >= 0 a <= b a.compareTo(b) <= 0 All comparisons are translated into calls to compareTo, that is required to return Int. 所以在我看来,没有办法使用操作符重载来表示一个向量为导向的比较操作。 我错过了什么吗? […]

在Kotlin中,我可以覆盖一些现有的运营商,但是如何创建新的运营商呢?

在Kotlin中,我看到我可以重写一些运算符,例如+ by plus()和* by函数times() …但是对于像Sets这样的一些东西,首选的(集合论)符号/运算符是不存在的。 例如A∪B为交集, A∪B为联合。 我似乎无法定义我自己的操作员,没有明确的语法来说明操作员使用什么符号。 例如,如果我想为$$作为一个运算符的函数: operator fun String.$$(other: String) = "$this !!whatever!! $other" // or even operator fun String.whatever(other: String) = "$this !!whatever!! $other" // how do I say this is the $$ symbol?!? 我得到两个相同的错误: 错误:(y,x)Kotlin:“运算符”修饰符不适用于此函数:非法函数名称 什么是运营商可以创建或重写的规则? 注意: 这个问题是由作者故意写的和回答的( 自我回答的问题 ),所以对于常见的Kotlin话题的习惯性的回答是在SO中。

Kotlin – 函数的调用操作符重载

我正在学习Kotlin – 运算符重载 我想了解(例如)运算符重载如何为函数的invoke()函数工作 预先测试 Kotlin的扩展函数 fun exampleOfExtensionFunction() { fun Int.randomize(): Int { return Random(this.toLong()).nextInt() } val randomizedFive = 5.randomize() println("$randomizedFive") } 打印: -1157408321 在Kotlin中,函数可以用类型声明为变量 fun exampleOfFunctionType() { val printNumber: (number: Int) -> Unit printNumber = { number -> println("[$number = ${number.toString(16).toUpperCase()} = ${number.toString(2)}]") } printNumber(1023) } 打印: [1023 = 3FF = 1111111111] Kotlin允许运算符使用扩展和成员函数进行重载 fun […]