# A puzzling result with a simple calculation

**URL:** https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118
**Category:** General Usage
**Created:** [September 6, 2024, 11:49am UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118 "2024-09-06T11:49:22Z")
**Posts on this page:** 17
**Page:** 2

<div class="post-metadata">

### Author: ![alemelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alemelis/32/208433_2.png) [@alemelis](https://discourse.julialang.org/u/alemelis)
#### Post date: [September 7, 2024, 6:49pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/21 "2024-09-07T18:49:17Z")

</div>

On the topic of juxtaposition but with strings, the following are all known behaviors

```julia
“Hello” * ” world” # -> “Hello world”

h = “Hello”
h” world” # -> UndefVarError: `@h_str` not defined 

w = “ world”
“Hello”w # -> ParseError:
# └ ── cannot juxtapose string literal

```

However, this one is puzzling

```julia
h[1:end]w # -> “Hello world”

```

I’d expect that to throw a parser error as well but instead the `*` is _correctly_ inferred

```julia
@show h[1:end]w # -> h[1:end] * w = "hello world"

```

---

<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: [September 7, 2024, 7:00pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/22 "2024-09-07T19:00:09Z")

</div>

`"foo"b` being an error is good because it would be confusing if it meant multiplication when `a"foo"b` is a nonstandard string literal with `b` as an extra argument.

---

<div class="post-metadata">

### Author: ![alemelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alemelis/32/208433_2.png) [@alemelis](https://discourse.julialang.org/u/alemelis)
#### Post date: [September 7, 2024, 7:01pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/23 "2024-09-07T19:01:37Z")

</div>

Totally agree, but also the last one should be an error (or not?)

---

<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: [September 7, 2024, 7:03pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/24 "2024-09-07T19:03:28Z")

</div>

I don’t see what the confusion would be?

It’s nice to have very uniform syntax where strings and arrays behave similarly but we don’t always get that.

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [September 7, 2024, 7:24pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/25 "2024-09-07T19:24:45Z")

</div>

> [@alemelis](#):
>
> However, this one is puzzling
> 
> ```julia
> h[1:end]w # -> “Hello world”
> 
> ```
> 
> I’d expect that to throw a parser error as well

That appears to be a general parsing behavior. (The parser doesn’t know that `h` is a string.)

```julia-repl
julia> :(A()w)
:(A() * w)

julia> :([1]w)
:([1] * w)

```

---

<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: [September 7, 2024, 7:53pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/26 "2024-09-07T19:53:45Z")

</div>

> [@jar1](#):
>
> I think for a number literal, juxtaposition is multiplication syntax not function-call syntax.

I know that (it’s in the OP, too). I just meant it’s not unavoidable that accepting `2x` means one has to accept `2(2+1)`.

---

<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: [September 7, 2024, 9:22pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/27 "2024-09-07T21:22:27Z")

</div>

I’m a physicist, and this is entirely expected behavior for me. If I mean `6/2*(2+1)` I will write it like that.

---

<div class="post-metadata">

### Author: ![tiZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tiz/32/211800_2.png) [@tiZ](https://discourse.julialang.org/u/tiZ)
#### Post date: [September 8, 2024, 9:07am UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/28 "2024-09-08T09:07:22Z")

</div>

same feeling here, I think if people accept 2x in Julia is (2 \times x), it is intuitive that `6/2(2+1)` = 6 \div 2(2+1) and `6/2*(2+1)` = 6 \div 2\times (2+1)

---

<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: [September 8, 2024, 3:10pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/29 "2024-09-08T15:10:06Z")

</div>

But it wouldn’t be inconsistent (but of course breaking) to disallow `2(2+1)`, since it can be interpreted as function call syntax. I find `2x` to be very nice syntax, but dislike `2(2+1)`.

The reason I make this point is that it was argued that one cannot have the former without the latter.

---

<div class="post-metadata">

### Author: ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)
#### Post date: [September 9, 2024, 4:55am UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/30 "2024-09-09T04:55:42Z")

</div>

I think that if you want to use Julia that if you write

1. 2x
2. 2(3)
3. 2(3.1415926+4)

You have to think of the whole thing as a single indivisible number.

Then you can write 1/2pi meaning 1/(2\*pi)

---

<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: [September 9, 2024, 6:23am UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/31 "2024-09-09T06:23:49Z")

</div>

Yes, that is the current situation. But I was saying that it is possible to like `2x` without liking `2(2+1)`. The language didn’t _have_ to have both. It _does_ have both, but it didn’t _have_ to.

---

<div class="post-metadata">

### Author: ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)
#### Post date: [September 9, 2024, 11:10am UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/32 "2024-09-09T11:10:27Z")

</div>

> [@DNF](#):
>
> But it wouldn’t be inconsistent (but of course breaking) to disallow `2(2+1)`, since it can be interpreted as function call syntax.

Wolfram language uses square brackets for function calls, apparently to distinguish from the implicit multiplication syntax. Julia chooses not to use a function call syntax that’s alien to programmers from other languages, though this pragmatic compromise makes the syntax a little bit more ambiguous. Personally, I prefer to explicitly type out `*` operators when using either language.

---

<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: [September 9, 2024, 11:35am UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/33 "2024-09-09T11:35:14Z")

</div>

Julia already uses square brackets for another purpose, though, namely `getindex`, so this would anyway not solve anything:

```julia
julia> 2[1]
2

julia> 2[2]
ERROR: BoundsError: attempt to access Int64 at index [2]

```

It would make sense to me if `2[x]` is `getindex`, `2x` is multiplication, and `2(x)` were function call syntax.

---

<div class="post-metadata">

### Author: ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)
#### Post date: [September 9, 2024, 11:44am UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/34 "2024-09-09T11:44:28Z")

</div>

That’s exactly why Wolfram Language uses double square brackets for array indexing: `a(2)` means `a*2`, `a[2]` means calling function `a` with argument `2`, and `a[[2]]` means the 2nd element of the array `a`. Fully avoiding syntax ambiguities related to implicit multiplication has caused a chain reaction which produces a LOT of strange syntax in Wolfram.

---

<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: [September 9, 2024, 11:47am UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/35 "2024-09-09T11:47:53Z")

</div>

> [@greatpet](#):
>
> `a[[2]]` means the 2nd element of the array `a`

That’s odd, and certainly not unambiguous. This looks like it should mean ‘calling function `a` with input argument `[2]`’

But do you see ambiguities in this:

> [@DNF](#):
>
> `2[x]` is `getindex`, `2x` is multiplication, and `2(x)` were function call syntax.

?

---

<div class="post-metadata">

### Author: ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)
#### Post date: [September 9, 2024, 11:55am UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/36 "2024-09-09T11:55:38Z")

</div>

Yeah, `a[[2]]` would have been ambiguous in Wolfram Lang if `[2]` means a vector of one element `2`, but Wolfram actually uses curly brackets instead for vectors, making its syntax even more unfamiliar to most people. By allowing syntax like `2(x+y)` in a fully unambiguous way, it’s surprising how much the rest of the syntax choices become constrained in Wolfram Lang.

> [@DNF](#):
>
> But do you see ambiguities in this:
> 
> `2[x]` is `getindex`, `2x` is multiplication, and `2(x)` were function call syntax.

This looks consistent to me, if you are willing to forgo the convenience of things like `2(x+y)`. I don’t mind typing `2*(x+y)`, and in fact, `2*x` either.

---

<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: [September 9, 2024, 12:05pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/37 "2024-09-09T12:05:11Z")

</div>

> [@greatpet](#):
>
> and in fact, `2*x` either

You would actually need `(2*x)` to use it under the `/`, which the main reason I prefer `2x`.

[Previous page](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118.md?page=1)
