# PEP 584: Survey of other languages (operator overload)

**URL:** <https://discuss.python.org/t/pep-584-survey-of-other-languages-operator-overload/977>\
**Category:** Ideas\
**Created:** [March 4, 2019, 8:41am UTC](https://discuss.python.org/t/pep-584-survey-of-other-languages-operator-overload/977 "2019-03-04T08:41:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![methane](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/methane/32/43_2.png) [@methane](https://discuss.python.org/u/methane)\
**Post date:** [March 4, 2019, 8:41am UTC](https://discuss.python.org/t/pep-584-survey-of-other-languages-operator-overload/977/1 "2019-03-04T08:41:59Z")

</div>

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

- `+` for concatenate `Array`: [https://docs.ruby-lang.org/en/2.6.0/Array.html#method-i-2B](https://docs.ruby-lang.org/en/2.6.0/Array.html#method-i-2B)
- `|`, `+` for union of `Set`: [https://docs.ruby-lang.org/en/2.6.0/Set.html#method-i-7C](https://docs.ruby-lang.org/en/2.6.0/Set.html#method-i-7C)
- No operator for merging `Hash`: [https://docs.ruby-lang.org/en/2.6.0/Hash.html#method-i-merge](https://docs.ruby-lang.org/en/2.6.0/Hash.html#method-i-merge)

## Swift

- `+` for concatenate `Array`: [https://developer.apple.com/documentation/swift/array/3126935](https://developer.apple.com/documentation/swift/array/3126935)
- No operator for union of `Set`: [https://developer.apple.com/documentation/swift/set#topics](https://developer.apple.com/documentation/swift/set#topics)
- No operator for merging `Dict`:  
[https://developer.apple.com/documentation/swift/dictionary#topics](https://developer.apple.com/documentation/swift/dictionary#topics)

## Rust

I’m not familiar with Rust so I’m not sure. But it seems implementing traits in [`core::ops`](https://doc.rust-lang.org/core/ops/) means overloading operator.

- No operator for concat `Vec`. Use `extend`: [https://doc.rust-lang.org/std/vec/struct.Vec.html#impl-Extend\<T\>](https://doc.rust-lang.org/std/vec/struct.Vec.html#impl-Extend%3CT%3E)
- `|` for union of `HashSet`: [https://doc.rust-lang.org/std/collections/struct.HashSet.html#impl-BitOr\<%26'b%20HashSet\<T%2C%20S\>\>](https://doc.rust-lang.org/std/collections/struct.HashSet.html#impl-BitOr%3C%26%27b%20HashSet%3CT%2C%20S%3E%3E)
- No operator for merging of `HashMap`. Use `extend`: [https://doc.rust-lang.org/std/collections/struct.HashMap.html#impl-Extend\<(K%2C%20V)\>](https://doc.rust-lang.org/std/collections/struct.HashMap.html#impl-Extend%3C(K%2C%20V)%3E)

## Scala

- `++` for concat `Seq`: [ref](https://www.scala-lang.org/api/current/scala/collection/Seq.html#++%5BB%3E:A,That%5D(that:scala.collection.GenTraversableOnce%5BB%5D)(implicitbf:scala.collection.generic.CanBuildFrom%5BRepr,B,That%5D):That)
- `|` for union of `Set`: [ref](https://www.scala-lang.org/api/current/scala/collection/Set.html#%7C(that:scala.collection.GenSet%5BA%5D):Repr)
- `++` for adding elements from iterator into Set: [ref](https://www.scala-lang.org/api/current/scala/collection/Set.html#++(elems:scala.collection.GenTraversableOnce%5BA%5D):This)
- `++` for merging `DefaultMap`: [ref](https://www.scala-lang.org/api/current/scala/collection/DefaultMap.html#++(xs:Traversable%5B(A,B)%5D):scala.collection.Map%5BA,B%5D)
- `+` for adding one or some key: value pairs to `DefaultMap` (e.g. Similar to Python’s `dict.update(key=val, key2=val2)` form). [ref](https://www.scala-lang.org/api/current/scala/collection/DefaultMap.html#+(kvs:(A,B)*):scala.collection.Map%5BA,B%5D)

Note:

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

## Kotlin

- `+` for concat `ArrayList`: [https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-array-list/index.html](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-array-list/index.html)
- `+` for union of `HashSet`: [https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-hash-set/index.html](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-hash-set/index.html)
- `+` for merging `HashMap`: [https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-hash-map/index.html](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-hash-map/index.html)

Note:

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

---

<div class="post-metadata">

**Author:** ![uranusjr](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/uranusjr/32/103_2.png) [@uranusjr](https://discuss.python.org/u/uranusjr)\
**Post date:** [March 4, 2019, 12:01pm UTC](https://discuss.python.org/t/pep-584-survey-of-other-languages-operator-overload/977/2 "2019-03-04T12:01:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cben](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cben/32/872_2.png) [@cben](https://discuss.python.org/u/cben)\
**Post date:** [November 12, 2019, 2:00pm UTC](https://discuss.python.org/t/pep-584-survey-of-other-languages-operator-overload/977/3 "2019-11-12T14:00:34Z")

</div>

Ruby hashes have no operators for this, but many methods (versions with `!` mutate in-place):

```ruby
pry(main)> a = {1 => 10}
=> {1=>10}
pry(main)> b = {2 => 20}
=> {2=>20}

# Merging:
pry(main)> a.merge(b)
=> {1=>10, 2=>20}
pry(main)> a
=> {1=>10}
pry(main)> a.merge!(b) # like Python's .update()
=> {1=>10, 2=>20}
pry(main)> a
=> {1=>10, 2=>20}

# Subset with given keys:
pry(main)> a.slice(1)
=> {1=>10}
pry(main)> a.slice(2)
=> {2=>20}
pry(main)> a
=> {1=>10, 2=>20}
pry(main)> a.slice!(1) # ActiveSupport extension
=> {2=>20}
pry(main)> a
=> {1=>10}

# Drop given keys
pry(main)> a.except(1) # ActiveSupport extension
=> {2=>20}
pry(main)> a
=> {1=>10, 2=>20}
pry(main)> a.except!(1) # ActiveSupport extension
=> {2=>20}
pry(main)> a
=> {2=>20}

```

It’s also interesting that Ruby arrays use `+` for concatenation, and at the same time `|`, `&` and `-` for set operations:

```ruby
pry(main)> [1, 2] + [3, 1]
=> [1, 2, 3, 1]
pry(main)> [1, 2] | [3, 1]
=> [1, 2, 3]
pry(main)> [1, 2] - [3, 1]
=> [2]
pry(main)> [1, 2] & [3, 1]
=> [1]

```

(It _also_ has a `Set` class supporting same operations except both `+` and `|` do union.)
