Proposal for an assign (copy) operator


#1

I'm pretty new to Kotlin but I'd like to share a small proposal that could IMHO benefit the language. Its implementation should be simple.

According to the latest documentation the following assign operators are currently supported:

Expression Translated To
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)

But in certain situations all you want to perform is a copy/assignment without a preceding arithmetic operation:

Proposed Expression Translated To
a := b a.copyAssign(b) // or a.valueAssign(b) // or simply a.assign(b)

This is unrelated to the standard assignment operator (=) that simply changes the object being referenced by the variable.

Hypothetical scenarios where it could be useful:

  • Copying Containers / clone-Into

      tableA := tableB           // tableA now has the same records as tableB
      arrayA := arrayB          // arrayA is still a separate object
                         

  • Operations without temporary objects:

      matrixA := matrixB * matrixC

where (matrixB * matrixC) does not yield a matrix object but an “operation” object that gets applied later during the := operator.

This second scenario is the main reason I came forward with this proposal.

Thank you for your time.


#2

Use cases seem rather narrow, but thanks for your proposal


#3

Yes, I almost didn't share it because of that. But then I realized the use cases for -= and %= are narrow too so I thought "why not".

Thanks for your time again,

Steven