# New overloading operator methods

**URL:** <https://discuss.kotlinlang.org/t/new-overloading-operator-methods/2007>\
**Category:** Language Design\
**Created:** [October 3, 2016, 9:52pm UTC](https://discuss.kotlinlang.org/t/new-overloading-operator-methods/2007 "2016-10-03T21:52:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![elect](https://avatars.discourse-cdn.com/v4/letter/e/e480ec/32.png) [@elect](https://discuss.kotlinlang.org/u/elect)\
**Post date:** [October 3, 2016, 9:52pm UTC](https://discuss.kotlinlang.org/t/new-overloading-operator-methods/2007/1 "2016-10-03T21:52:02Z")

</div>

I am writing a glm java porting [here](https://github.com/java-graphics/glm) and looking for an overloading operator support I stumbled over Kotlin and I fell in love with it…

There is only one problem I am facing right now… I’d like to have the case where I perform this addition

```
var a = Vec2(1f, 2f)
var b = Vec2(3f, 4f)
var c = a + b

operator fun plus(other: Vec2): Vec2 = Vec2(x + other.x, y + other.y)

```

where c is a new instance… no problem so far. But I’d also like to have support for the case when c is already initialized and I just want to overwrite its x and y variables…

```
var a = Vec2(1f, 2f)
var b = Vec2(3f, 4f)
var c = Vec2()
c.set(a + b)

operator fun plus(other: Vec2): FloatArray = floatarrayof(x + other.x, y + other.y)

```

The big problem here is that I cannot have simultaneously these two operator overloading functions…

Do you have a tip how I could find a solution (even tricky) about it?

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [October 3, 2016, 9:58pm UTC](https://discuss.kotlinlang.org/t/new-overloading-operator-methods/2007/2 "2016-10-03T21:58:49Z")

</div>

Why don’t you just use `Vec2` as an argument of `set()`?  
What is the advantage of having `plus` returning `FloatArray` and not `Vec2`?

---

<div class="post-metadata">

**Author:** ![elect](https://avatars.discourse-cdn.com/v4/letter/e/e480ec/32.png) [@elect](https://discuss.kotlinlang.org/u/elect)\
**Post date:** [October 3, 2016, 10:01pm UTC](https://discuss.kotlinlang.org/t/new-overloading-operator-methods/2007/3 "2016-10-03T22:01:49Z")

</div>

In order to minimize the garbage collector activity…

There are some situations where I don’t want to have any garbage (or at least as less as possible), that is, if I have a pool of Vec2, I can re-use for example c to save the result of a + b

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [October 3, 2016, 10:05pm UTC](https://discuss.kotlinlang.org/t/new-overloading-operator-methods/2007/4 "2016-10-03T22:05:16Z")

</div>

`FloatArray` as a result also requires an allocation.  
Anyway you couldn’t have an overload of a function that differs only in return type, so you have to choose another name for `plus` that returns an array.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [October 3, 2016, 10:11pm UTC](https://discuss.kotlinlang.org/t/new-overloading-operator-methods/2007/5 "2016-10-03T22:11:33Z")

</div>

You could provide an operator `+=` in `Vec2`:

```auto
class Vec2 {
    operator fun plusAssign(other: Vec2): Unit {
         this.x += other.x
         this.y += other.y
    }
}

```

and then you can add vectors in-place with no allocations as following:

```auto
var a = Vec2(1f, 2f)
var b = Vec2(3f, 4f)
val c = Vec2(a) // copy constructor 
// or c = a.copy() if Vec2 is a data class
c += b

```

But now you have to declare `c` as `val`, since having both `plus` and `plusAssign` available on a mutable variable leads to an ambiguity of expression `c += b`.

---

<div class="post-metadata">

**Author:** ![elect](https://avatars.discourse-cdn.com/v4/letter/e/e480ec/32.png) [@elect](https://discuss.kotlinlang.org/u/elect)\
**Post date:** [October 5, 2016, 12:15pm UTC](https://discuss.kotlinlang.org/t/new-overloading-operator-methods/2007/6 "2016-10-05T12:15:15Z")

</div>

I’d like to have the possibility to override basic operations as following

```
a = b + c

operator fun addition(a: Vec2, b: Vec2): Vec2 {
    x = a.x + b.x
    y = a.y + b.y
    return this;
}

```

in order to save result in `a` without creating any intermediate garbage result…

According to Perry Nguyen, this is possible in Scala:

[quote=“Perry Nguyen, post:6, topic:2007, full:true”]it would end up being something like a := b / c, where := would be defined as `def :=(ops: Vec2Op) = a.append(ops.calculate)` and `b / c` would evaluate as a Vec2Op  
[/quote]

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [October 10, 2016, 6:54pm UTC](https://discuss.kotlinlang.org/t/new-overloading-operator-methods/2007/7 "2016-10-10T18:54:50Z")

</div>

[quote=“Perry Nguyen”]it would end up being something like a := b / c, where := would be defined as `def :=(ops: Vec2Op) = a.append(ops.calculate)` and `b / c` would evaluate as a Vec2Op  
[/quote]

Note that `Vec2Op` also could be considered as intermediate garbage result.
