# Minus sign subtracts strings

**URL:** https://es.discourse.group/t/minus-sign-subtracts-strings/1050
**Category:** 🦋 Proposals
**Created:** [October 22, 2021, 1:00pm UTC](https://es.discourse.group/t/minus-sign-subtracts-strings/1050 "2021-10-22T13:00:49Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![jaroslav.tavgen](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jaroslav.tavgen/32/2732_2.png) [@jaroslav.tavgen](https://es.discourse.group/u/jaroslav.tavgen)
#### Post date: [October 22, 2021, 1:00pm UTC](https://es.discourse.group/t/minus-sign-subtracts-strings/1050/1 "2021-10-22T13:00:49Z")

</div>

"1234567" + "89" // '123456789'  
"1234567" - "345" // '1267'

(inspired by [https://mobile.twitter.com/jasmerchant/status/1451244887228043269](https://mobile.twitter.com/jasmerchant/status/1451244887228043269))

---

<div class="post-metadata">

### Author: ![lightmare](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lightmare/32/843_2.png) [@lightmare](https://es.discourse.group/u/lightmare)
#### Post date: [October 22, 2021, 1:07pm UTC](https://es.discourse.group/t/minus-sign-subtracts-strings/1050/2 "2021-10-22T13:07:20Z")

</div>

This is DOA, because you cannot change what `a-b` does.  
But even if you could, why break `a-b` instead of fixing broken `a+b` ?

---

<div class="post-metadata">

### Author: ![beck](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/beck/32/1241_2.png) [@beck](https://es.discourse.group/u/beck)
#### Post date: [February 16, 2022, 4:22pm UTC](https://es.discourse.group/t/minus-sign-subtracts-strings/1050/3 "2022-02-16T16:22:36Z")

</div>

> fixing broken `a+b`

For my own curiosity, how do you perceive it as broken? Doing a small survey of other languages, concatenation seem to be the norm.

---

<div class="post-metadata">

### Author: ![lightmare](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lightmare/32/843_2.png) [@lightmare](https://es.discourse.group/u/lightmare)
#### Post date: [February 17, 2022, 10:51pm UTC](https://es.discourse.group/t/minus-sign-subtracts-strings/1050/4 "2022-02-17T22:51:56Z")

</div>

> [@beck](#):
>
> For my own curiosity, how do you perceive it as broken? Doing a small survey of other languages, concatenation seem to be the norm.

In other languages I know, either `+` has a given function and coerces arguments, or `+` can be overloaded and its function determined by argument types. In JS `+` cannot be overloaded, its function is determined by argument values, and arguments are coerced at the same time.

For example in AWK, Perl, Lua, `+` is always addition. They coerce arguments to numbers (each has their own rules how). AWK doesn't have concatenation operator symbol, it simply concatenates adjacent expressions, Perl has `.` , Lua has `..`

Perl even has separate arithmetic and string comparison operators: `==` vs `eq`  
and a `string x number` multiplication (repeat) operator, vs arithmetic `number * number`

Haskell has `+` for addition, `++` for concatenation, iirc.

In C++, Python, `+` is used for both addition and concatenation; but built-in overloads prevent you from mismatching argument types. In C++ the operator function is determined from argument types (and possibly implicit conversions). In Python, strictly speaking, you can have different overloads directly on values, but I haven't seen any code do that; overloads are customarily defined on types.

> **example grievance with JS +**
>
> ```js
> console.log((n + 1) + " fish")
> 
> ```
> 
> (wasn't allowed to use template strings, otherwise I would, of course)  
> Each of those `+` means something else. And it was made worse by "prettier", which was mandatory on that project and removed the inner parentheses because they're not needed for precedence. But imo they helped clarity.
> 
> In a sensible language, you'd have some way to distinguish addition from concatenation, or be forced to explicitly state your intent with conversion:  
> Perl: `($n + 1) . " fish"`  
> AWK: `(n + 1) " fish"`  
> Lua: `(n + 1) .. " fish"`  
> Python: `str(n + 1) + " fish"`
> 
> Admittedly the last one is almost as bad as JS. Luckily both Python and JS now have a much better way to accomplish this, with format/template strings.

---

<div class="post-metadata">

### Author: ![beck](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/beck/32/1241_2.png) [@beck](https://es.discourse.group/u/beck)
#### Post date: [February 18, 2022, 12:12am UTC](https://es.discourse.group/t/minus-sign-subtracts-strings/1050/5 "2022-02-18T00:12:16Z")

</div>

Operator overload is super fun and I can see the utility in a concat operator. I'm not convinced that `+` is broken without these (rad) features. I think this is more-so a critique for dynamically typed languages, the behavior of `n + 1` is undeterminable without knowing the type of `n`.

---

<div class="post-metadata">

### Author: ![claudepache](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudepache/32/236_2.png) [@claudepache](https://es.discourse.group/u/claudepache)
#### Post date: [February 18, 2022, 3:52pm UTC](https://es.discourse.group/t/minus-sign-subtracts-strings/1050/6 "2022-02-18T15:52:13Z")

</div>

> [@beck](#):
>
> I think this is more-so a critique for dynamically typed languages, the behavior of `n + 1` is undeterminable without knowing the type of `n`.

No, it is not an issue in dynamically typed language where `+` means only “addition of numbers” (in which case `n + 1` means always “coerce `n` to a number, then add `1`”).

---

<div class="post-metadata">

### Author: ![beck](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/beck/32/1241_2.png) [@beck](https://es.discourse.group/u/beck)
#### Post date: [February 23, 2022, 7:15pm UTC](https://es.discourse.group/t/minus-sign-subtracts-strings/1050/7 "2022-02-23T19:15:05Z")

</div>

Sorry, can you clarify:

> `+` means only “addition of numbers” (in which case `n + 1` means always “coerce `n` to a number, then add `1` ”).

Is this the behavior that is desired or detested?

---

<div class="post-metadata">

### Author: ![Rudxain](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/rudxain/32/2272_2.png) [@Rudxain](https://es.discourse.group/u/Rudxain)
#### Post date: [September 11, 2022, 2:01am UTC](https://es.discourse.group/t/minus-sign-subtracts-strings/1050/9 "2022-09-11T02:01:27Z")

</div>

This is a bad idea for JS, because `str - str` is valid and already defined as subtraction with coercion `toNumber`. However, this is a good idea for an esolang.

But how does it work exactly? Does it remove substrings or chars? If it removes chars, how is a "char" defined? (code-unit, code-point, or grapheme cluster?) What happens if the chars to be removed aren't present in the left-side string?
