Tag: 缀记法

中缀符号和(…)不能正常工作

考虑以下情况:我有一个类Test class Test() { infix fun say(msg: String) = println(msg) } 和一个主要方法 fun main(args: Array<String>) { val test = Test() test say "Hello World!" //Works with(test) { say "Goodbye World!" //Does not work say("Hello again!") //Works } } 正如你所看到的,我正在测试中缀符号。 考虑with(…)允许你使用在with块中作为参数传递的对象,而不必通过点符号访问它的成员,我期望中缀表示法像我在上面的示例中显示的那样工作。 不幸的是,这是行不通的,有没有原因,这是行不通的? 这是一个错误还是一个限制? 或者,也许我不正确解释with(…)功能?

我们可以在Kotlin中使用infix通用方法吗?

编译器接受中缀+泛型方法,但是使用它的语法是什么? 例如,给定那些2个相同的方法(模随机泛型): infix inline fun Int1.plus1(i: Int1) = Int1(this.value + i.value) infix inline fun <U> Int1.plus2(i: Int1) = Int1(this.value + i.value) 我可以写: Int1(3).plus1(Int1(4)) Int1(3) plus1 Int1(4) Int1(3).plus2<Int>(Int1(4)) 但是这个电话是无效的: Int1(3) plus2<Int> Int1(4) 有人可以解释我为什么?

Android kotlin中缀问题

我正在得到一个有趣的问题。 当我调试我的应用程序isResColorId是false 。 不幸的是, let函数被触发,并且我看到了logcat上的qwe 。 fun drawableTint(context: Context, view: View, colorID: Int, isResColorId: Boolean = true) { try { val wrap = DrawableCompat.wrap(view.background) DrawableCompat.setTint(wrap, isResColorId then let { Timber.d("qwe"); ContextCompat.getColor(context, colorID) } ?: colorID) view.setBackgroundDrawable(wrap) }catch (e: Resources.NotFoundException){ Timber.e(e, "c_id: $colorID coz: $isResColorId") } } infix fun <T> Boolean.then(param: T): T? = if (this) […]