PEP 584: Survey of other languages (operator overload)


(Inada Naoki) #1

I surveyed other popular languages having operator overload. This topic is not for voting PEP 584. Just for gathering one of information for making a decision.

I used here instead of ML to update this post by reply.

C++

C++ doesn’t use operator overload for containers much.

  • No operator for concatenate vectors
  • No operator for union of set
  • No operator for merging maps

C#

Like C++, C# doesn’t use operator overload for containers.

Ruby

Swift

Rust

I’m not familiar with Rust so I’m not sure. But it seems implementing traits in core::ops means overloading operator.

Scala

  • ++ for concat Seq: ref
  • | for union of Set: ref
  • ++ for adding elements from iterator into Set: ref
  • ++ for merging DefaultMap: ref
  • + for adding one or some key: value pairs to DefaultMap (e.g. Similar to Python’s dict.update(key=val, key2=val2) form). ref

Note:

  • Scala uses + for adding one or some elements, and ++ for adding elements from iterator.

Kotlin

Note:

  • Kotlin can overload + but |. So it didn’t mean Kotlin chose + over |.
  • - of HashMap takes key or iterator of keys, not Map.

(Tzu-ping Chung) #2

Rust’s API is heavily iterator based, so it’s something like a.into_iter().chain(b).collect(). There’s no overloaded operator for this, but the reason is more complicated than a design decision due to memory management.