java operator overload_Operator Overload in Kotlin: Add standard operations to any class (KAD 17)

In Kotlin, as in every language, we have predefined operators to perform certain operations.

The most typical are the addition (+), subtraction (-), multiplication (*) or division (/), but there are a few more.

In some languages, such as Java, these operators are limited to certain types of data, and we have no way to make other types use them.

There are other languages like Scala in which we can invent any type of operator that occurs to us, since the names of the functions accept any symbol.

An intermediate solution is taken in Kotlin: there are a number of predefined operators, but we can overload them for any kind of data .

Operator Overload in Kotlin

As we talked, Kotlin can overload a number of operators, implementing the corresponding function in our class. This function must be marked with the reserved word operator .

The operators are basically the following:

Unary Operators

+a

a.unaryPlus()

-a

a.unaryMinus()

!a

a.not()

a++

a.inc()

a–

a.dec()

Binary Operators

a + b

a.plus(b)

a – b

a.minus(b)

a * b

a.times(b)

a / b

a.div(b)

a % b

a.mod(b)

a..b

a.rangeTo(b)

a in b

b.contains(a)

a !in b

!b.contains(a)

a += b

a.plusAssign(b)

a -= b

a.minusAssign(b)

a *= b

a.timesAssign(b)

a /= b

a.divAssign(b)

a %= b

a.modAssign(b)

Array type operators

a[i]

a.get(i)

a[i, j]

a.get(i, j)

a[i_1, …, i_n]

a.get(i_1, …, i_n)

a[i] = b

a.set(i, b)

a[i, j] = b

a.set(i, j, b)

a[i_1, …, i_n] = b

a.set(i_1, …, i_n, b)

Equals Operation

a == b

a?.equals(b) ?: b === null

a != b

!(a?.equals(b) ?: b === null)

The equals operations are a bit different, because they use a more complex translation to do a correct check, and because they expect an exact specification of the function and not just a specific name for it. The function must be implemented exactly like this:

funequals(other: Any?): Boolean

Invoking Functions

a(i)

a.invoke(i)

a(i, j)

a.invoke(i, j)

a(i_1, …, i_n)

a.invoke(i_1, …, i_n)

An example

Imagine that you have a data model for companies, each of which has a list of employees.

You could use the get operator to access the positions using brackets. The implementation is very easy:

class Employee(valid: Long, valname: String)

class Company(private valemployees: List) {

operatorfunget(pos: Int) = employees[pos]

}

And that’s how you could use it:

valcompany = Company(listOf(Employee(1235, "John"), Employee(2584, "Mike")))

valmike = company[1]

But you could go beyond, and use the id to recover the value, implementing the function like this:

operatorfunget(id: Long) = employees.first { it.id == id }

valmike = company[2584]

Conclusion

Operators can help us to improve readability by using common symbols for known operations.

Be careful not to use them in situations where they can lead to confusion to readers, and they can become a really interesting tool.

If you have not already done so, I recommend you toget the free guide to learn how to build your first project, or justget the book and learn how to create a complete App from scratch.

I’m in love with Kotlin. I’ve been learning about it for a couple of years, applying it to Android and digesting all this knowledge so that you can learn it with no effort.

注意:本文来自网友投稿。本站无法对本文内容的真实性、完整性、及时性、原创性提供任何保证,请您自行验证核实并承担相关的风险与后果!

CoLaBug.com遵循[CC BY-SA 4.0]分享并保持客观立场,本站不承担此类作品侵权行为的直接责任及连带责任。您有版权、意见、投诉等问题,请通过[eMail]联系我们处理,如需商业授权请联系原作者/原网站。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值