# Why are there all these strange stumbling blocks in Julia?

**URL:** https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644
**Category:** New to Julia
**Tags:** syntax, python
**Created:** [January 7, 2023, 6:19pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644 "2023-01-07T18:19:30Z")
**Posts on this page:** 20
**Page:** 2

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [January 7, 2023, 11:16pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/21 "2023-01-07T23:16:56Z")

</div>

You mean things like `"abc"^3 == "abcabcabc"`? Yes.

Conceptually, if not practically, I would love `"start" \"startmiddleend"/"end" == "middle"`

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [January 7, 2023, 11:18pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/22 "2023-01-07T23:18:36Z")

</div>

```julia
julia> oneunit(String) * "ab"
"ab"

```

---

<div class="post-metadata">

### Author: ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)
#### Post date: [January 7, 2023, 11:44pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/23 "2023-01-07T23:44:09Z")

</div>

The practical example I have in mind is that, because regular expressions follow a [Kleene algebra](https://en.wikipedia.org/wiki/Kleene_algebra) in which concatenation is represented by (\cdot), then because we have chosen `*` for concatenation we can implement operations on regular expressions and they will be consistent with their theoretical properties. For example, we could implement an alternation operator for regular expressions using `+` as is common in formal language theory (or another `+` like operator such as `|`).

If we did implement more operators for regular expressions, it’d be a crying shame if string operations were dissimilar and had different operator precedence.

---

<div class="post-metadata">

### Author: ![Sandjan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sandjan/32/45684_2.png) [@Sandjan](https://discourse.julialang.org/u/Sandjan)
#### Post date: [January 8, 2023, 9:26am UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/24 "2023-01-08T09:26:54Z")

</div>

I have already expected this answer. I know Julia is not and should not be a Python clone, but my underlying question is: why reinvent things that worked easily and intuitively in another language? For the most part, @DNF has given understandable answers to my specific points.

---

<div class="post-metadata">

### Author: ![fph](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fph/32/17159_2.png) [@fph](https://discourse.julialang.org/u/fph)
#### Post date: [January 8, 2023, 9:27am UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/25 "2023-01-08T09:27:12Z")

</div>

> [@giordano](#):
>
> Try `"hello" ^ 5`

Frankly this example and `oneunit(String) * "ab"` are not very convincing because they would work in the exact same way with addition: `"hello" * 5`, `zerounit(String) + "ab"`. On the other hand I am fully convinced by `"start" \ "startmiddleend" / "end"`, because it exploits fully the fact that multiplication is not commutative and has two inverses. And by the example with regexp’s and commutativity. Thanks to @gustaphe and @uniment, I was glad to see your examples.

Too bad these two are only suggestions and do not work in practice. It would have been great to have an example of a currently existing practical feature of the language / standard library which is enabled by the choice of `*` rather than `+`; if you have such an example I am all ears.

---

<div class="post-metadata">

### Author: ![Sandjan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sandjan/32/45684_2.png) [@Sandjan](https://discourse.julialang.org/u/Sandjan)
#### Post date: [January 8, 2023, 9:38am UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/26 "2023-01-08T09:38:56Z")

</div>

I see this from a more reality-based perspective, if I have two apples and add one, I have three apples. I can also multiply apples times 2, then I have four apples. Therefore it is difficult for me to understand this \*, because I cannot multiply apples among each other and do not expect to get the result of an addition. This is very unintuitive for me.

---

<div class="post-metadata">

### Author: ![Sandjan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sandjan/32/45684_2.png) [@Sandjan](https://discourse.julialang.org/u/Sandjan)
#### Post date: [January 8, 2023, 9:42am UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/27 "2023-01-08T09:42:09Z")

</div>

“elif” simply because it is understood just as well, but is shorter.

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [January 8, 2023, 9:42am UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/28 "2023-01-08T09:42:23Z")

</div>

> [@Sandjan](#):
>
> but my underlying question is: why reinvent things that worked easily and intuitively in another language?

This has been said before, but once again:

1. There are many other languages (you just happen to have a strong Python bias).
2. “easily and intuitively” is largely subjective and heavily influenced by the language you’re coming from.

For example, `elseif` is “easy and intuitive” in MATLAB, which also uses 1-based-indexing.

---

<div class="post-metadata">

### Author: ![Sandjan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sandjan/32/45684_2.png) [@Sandjan](https://discourse.julialang.org/u/Sandjan)
#### Post date: [January 8, 2023, 9:50am UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/29 "2023-01-08T09:50:21Z")

</div>

I have programmed Java before Python. So “yes” I found the Python syntax a great relief from the bloated Java syntax. I don’t stick to a language like a tribe I have to defend, I see what is practical and what is not, so these two sentences are relatively independent.

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [January 8, 2023, 9:50am UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/30 "2023-01-08T09:50:22Z")

</div>

> [@Sandjan](#):
>
> “elif” simply because it is understood just as well, but is shorter.

If I would ask my girlfriend (who knows nothing about programming) what `elif` means she might have no clue.

Anyways, I’m going to stop here, because such a syntax discussion is neither fun (at least not for me) nor useful (it is what it is and it won’t change.).

---

<div class="post-metadata">

### Author: ![Sandjan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sandjan/32/45684_2.png) [@Sandjan](https://discourse.julialang.org/u/Sandjan)
#### Post date: [January 8, 2023, 9:53am UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/32 "2023-01-08T09:53:40Z")

</div>

“elif” is very quick to understand and easy to remember, but you’re right, of course I’ll still learn Julia because the language just has so many other advantages!

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [January 8, 2023, 11:11am UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/33 "2023-01-08T11:11:16Z")

</div>

@Sandjan, I think your questions are reasonable and @DNF’s answer ([Why are there all these strange stumbling blocks in Julia? - #2 by DNF](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/2)) was a straightforward response that (you, presumably) acknowledged. So in a sense perhaps not much else needs to be said.

But I do care about how “learnable” Julia is and so these discussions are relevant. Areas of Julia that concern me most are things like the stacktraces and how complex types can occasionally become in Julia, but I notice that none of these made your list. It’s useful to hear the perspective of a newcomer, so thanks.

One point I can address directly, as an amusing discovery I made within my first 5 minutes of learning Python (unlike you, I learned Python after Julia, and perhaps unsurprisingly I have your experience in reverse when I look at Python code). This example illustrates how two sensible “conveniences” can in fact lead to inconsistency:

> [@Sandjan](#):
>
> Why is the type conversation not as short and sweet as in Python int(“4”)?

In Python,

```julia
>>> int("4") # seems fine
4
>>> float("3.2") # seems fine
3.2
>>> bool("False") # huh?
True

```

There’s a good reason for the behavior of the last line: unlike Julia, Python automatically tests “truthiness” (which some might argue is an advantage of Python) and for containers truth means “non-empty”. So because `"False"` is not the empty string, it’s `True`. But obviously that result is a trap if you expect it to parse the string.

In general, I find Python slightly too willing to just mush forward, and often I’d rather it just throw an error. It’s not as if Julia doesn’t have it’s own [amusing/concerning list of “wat?s”](https://miguelraz.github.io/blog/juliawats/), but at least in this particular case I think Julia’s increased pickiness saves you from a trap.

Anyway, I think it’s great that you’re paying attention to what aspects of Julia make it easier or harder to learn & use, and I’d be curious how your perspective on this evolves with increased familiarity with the language.

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [January 8, 2023, 2:26pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/34 "2023-01-08T14:26:52Z")

</div>

How come these “wats” do not break regular Julia code?

Is it because they are “weird” patterns one rarely stumbles into?

---

<div class="post-metadata">

### Author: ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)
#### Post date: [January 8, 2023, 2:46pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/35 "2023-01-08T14:46:24Z")

</div>

For the same reason that [these wats](https://github.com/cosmologicon/pywat) don’t break regular python code. ~~These~~ Most are just weird edge cases that you pretty much never see in the wild.

Edit: I did not want to dismiss the possibility that there are bugs happening because of these behaviors. But it’s not worse than for any other language IMHO. To elaborate, from the list linked by Tim Holy I consider only one entry (precedence of the range operator) a common footgun & two more entries as possible candidates for bugs that may appear in the wild

> **details**
>
> (typo changing \_ to - and accidentally shadowing `Base.:-` and maybe ambigous float literal juxtaposition if somebody manages to combine questionable variable naming, questionable whitespace placement and complete unawareness of scientific notation)

As always, it’s good to be aware of the edge cases of any language you work with.

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [January 8, 2023, 7:22pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/36 "2023-01-08T19:22:30Z")

</div>

The parsing precedence of `:` and of `&`/`==` causes bugs all the time. Others like `fld`/`div` mentioned above may cause bugs as well.

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [January 8, 2023, 8:35pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/37 "2023-01-08T20:35:49Z")

</div>

> [@jar1](#):
>
> The parsing precedence

I tend to be very liberal in my use of parentheses for this reason. I prefer being explicit.

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [January 8, 2023, 9:19pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/38 "2023-01-08T21:19:59Z")

</div>

The answer by @DNF is straightforward, every language has its own choices and doesn’t have to be a copy of any other language. However, I find two valid points in the OP:

> [@Sandjan](#):
>
> Why is the type conversation not as short and sweet as in Python int(“4”)?

I find this truly annoying. I don’t mean specifically parsing, I mean something like _truncated integer division_. In C and Fortran, I could do for example `int x = 5.2 / 2` to get `x = 2`. To do the same thing in Julia, I have to write `x = trunc(Int, 5.2 / 2)`, there is no straightforward function/symbol for _truncated integer conversion_, and `Int(2.6)` doesn’t help here. Whereas in C and Fortran you get it implicitly for free based on the types of variables.

> [@Sandjan](#):
>
> Why do I concatenate strings with \* instead of + ? (that makes no sense to me at all)

Again, this is also a valid point. Using `*` for concatenating two strings is completely _unintuitive_, regardless of “commutativity” or any other algebraic property. No one (I assume) would think of stacking two strings beside each other using `*` instead of `+`. Simplicity matters, but Julia, at many times, chooses pickiness over simplicity.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [January 8, 2023, 9:24pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/39 "2023-01-08T21:24:25Z")

</div>

> [@Seif\_Shebl](#):
>
> No one (I assume) would think of stacking two strings beside each other using `*` instead of `+`.

This is _only_ about familiarity, not intuition. Intuitively, concatenation is closer to multiplication than to addition, in mathematics that is even how you normally _write_ multiplication: by concatenating symbols. xy is the product of x and y.

---

<div class="post-metadata">

### Author: ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)
#### Post date: [January 8, 2023, 9:42pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/40 "2023-01-08T21:42:05Z")

</div>

I think this is an “abstract algebra” v colloquial usage issue.

For non-mathematicians “x” concatenated with “x” gives “xx” and its natural to think of this as “two x’s”, hence + feels right.

In abstract algebra \* is just an operation that satisfies different properties (I think “free group” and “word” are relevant terms). Since concatenation satisfies these properties it makes sense to use \*.

Since Julia was designed with scientific computing (or numerical mathematics) in mind it makes a lot of sense they went with the mathematical version.

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [January 8, 2023, 10:27pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/41 "2023-01-08T22:27:41Z")

</div>

Isn’t it unusual for a `*` to have `measure(s * t) == measure(s) + measure(t)`, where `measure` is `length`?

[Previous page](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644.md?page=1)

[Next page](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644.md?page=3)
