# Whitespace sensitivity

**URL:** <https://discourse.julialang.org/t/whitespace-sensitivity/76211>\
**Category:** Internals & Design\
**Created:** [February 11, 2022, 9:05am UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211 "2022-02-11T09:05:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [February 11, 2022, 9:05am UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211/1 "2022-02-11T09:05:29Z")

</div>

When is Julia whitespace sensitvie?

- There are line endings to finish an expression, can be replaced with `;`.
- I recently came across

```julia
julia> [randn(2, 3)
                randn(2, 3)]
4×3 Matrix{Float64}:
 -0.0609315 0.342677 -0.173229
 -0.0603575 3.33838 0.229967
  0.894316 1.3177 -2.06582
  2.52888 0.0259237 -1.24774

julia> [randn(2, 3) randn(2, 3)]
2×6 Matrix{Float64}:
  1.40693 0.0107389 0.471341 1.97648 0.69173 -1.1349
 -0.622517 0.268651 -0.78219 0.518276 -0.460273 2.03445

```

are there more?

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [February 11, 2022, 9:17am UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211/2 "2022-02-11T09:17:31Z")

</div>

```julia
julia> [1.0 2.0 3.0 -4.0]
1×4 Matrix{Float64}:
 1.0 2.0 3.0 -4.0

julia> [1.0 2.0 3.0 - 4.0]
1×3 Matrix{Float64}:
 1.0 2.0 -1.0

```

I run into this from time to time. But it’s not really something different to what you showed already.

```julia
julia> [1.0; 2.0; 3.0 - 4.0]
3-element Vector{Float64}:
  1.0
  2.0
 -1.0

julia> [1.0; 2.0; 3.0 -4.0]
ERROR: ArgumentError: argument count does not match specified shape (expected 3, got 4)
Stacktrace:
 [1] hvcat(::Tuple{Int64, Int64, Int64}, ::Float64, ::Vararg{Float64})
   @ Base ./abstractarray.jl:1988
 [2] top-level scope
   @ REPL[9]:1

```

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [February 11, 2022, 10:46am UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211/3 "2022-02-11T10:46:34Z")

</div>

I am sure there is a lot of stuff you can do with `:`

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [February 11, 2022, 11:25am UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211/4 "2022-02-11T11:25:57Z")

</div>

Macro invocation is also sensitive to whitespace if the expressions are enclosed in parentheses:

[https://docs.julialang.org/en/v1/manual/metaprogramming/#Macro-invocation](https://docs.julialang.org/en/v1/manual/metaprogramming/#Macro-invocation)

---

<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:** [February 11, 2022, 1:19pm UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211/5 "2022-02-11T13:19:58Z")

</div>

That’s a standard array syntax like MATLAB and is very useful visually and when copying from a textbook for example. Functions like `vcat` and `hcat` do the same thing. The worst whitespace issue in Julia in my opinion still is something like `rand (2, 3)` is not allowed.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 11, 2022, 1:51pm UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211/6 "2022-02-11T13:51:46Z")

</div>

Why? Using a parentheses immediately after the function name seems kinda ubiquitous.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 11, 2022, 1:58pm UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211/7 "2022-02-11T13:58:29Z")

</div>

> [@Seif\_Shebl](#):
>
> `rand (2, 3)` is not allowed

this is horrendous and should not be allowed

---

<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:** [February 11, 2022, 3:13pm UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211/8 "2022-02-11T15:13:05Z")

</div>

I don’t have any statistics to assess how ubiquitous this is, but being able to write `f ( x )` in any language (at least those I know of) but not in Julia seems weird enough. I saw many code bases that use that without restrictions. It also resembles plain English. Functions in Julia are first class citizens and to my limited knowledge this restriction came from allowing macros to act on tuples. I guess putting restrictions on macros would have been better than putting restrictions on function. Any way, I’m sure that all decisions made by the respectable Julia developers and awesome community are only taken after extensive discussions, so if the community chooses that, it is very welcome.

---

<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:** [February 11, 2022, 3:14pm UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211/9 "2022-02-11T15:14:47Z")

</div>

Could you please elaborate why this is horrendous?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 11, 2022, 3:15pm UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211/10 "2022-02-11T15:15:34Z")

</div>

no, but I can point to the fact that all libraries you look, you will find people doing `f(x)` and PEP8 uses `f(x)` (if you’re thinking about python, since clearly this is not allowed in Julia)

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 11, 2022, 3:20pm UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211/11 "2022-02-11T15:20:45Z")

</div>

Many things in Julia are space-sensitive. For example `ab` is not the same as `a b`. What I suspect you’re asking about is when horizontal space is used to separate “arguments” and there are two contexts where this happens:

1. In array concatenation, horizontal whitespace separates horizontally concatenated blocks;
2. In parens-less macro calls, arguments are separated by horizontal whitespace (cf. macro calls that use parens, which are like function calls and use commas to separate arguments)

The rules for both of these contexts are the same and have the same caveats: `1+2` is the same as `1 + 2` is the same as `1+ 2` but different from `1 +2`. You can test it like this:

```julia
julia> macro args(exprs...) exprs end
@args (macro with 1 method)

julia> @args 1+2
(:(1 + 2),)

julia> @args 1+ 2
(:(1 + 2),)

julia> @args 1 + 2
(:(1 + 2),)

julia> @args 1 +2
(1, 2)

```

The same is true for any operator that can be both unary prefix and binary infix.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 11, 2022, 3:30pm UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211/12 "2022-02-11T15:30:46Z")

</div>

Opinions about whether this is ugly or improves clarity are besides the point really. Calling a function as `f ( x )` was originally allowed in Julia syntax, but it caused a syntax ambiguity in macro calls, so it was disallowed. The ambiguity, if I recall correctly, was that we allow macros to be called either as `@m a b` or as `@m(a, b)`, both of which are useful in different cases. But then what does `@m (a, b)` mean? Is it a call with two arguments or a call with a single tuple argument? If you disallow the whitespace between the function and its arguments in function call syntax, then this ambiguity is resolved (it has to be a macro with a single argument that is a tuple).

---

<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:** [February 11, 2022, 3:50pm UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211/13 "2022-02-11T15:50:18Z")

</div>

Yes, that’s totally fine. I’m not sure if the macros issue could’ve been solved using `Tuple(..)`?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 11, 2022, 4:08pm UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211/14 "2022-02-11T16:08:36Z")

</div>

That’s certainly a way to indicate that you want to pass a tuple, but it’s a different syntax which does matter for macros, and it doesn’t help disambiguate the other syntax.

---

<div class="post-metadata">

**Author:** ![ptoche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ptoche/32/23554_2.png) [@ptoche](https://discourse.julialang.org/u/ptoche)\
**Post date:** [February 11, 2022, 4:21pm UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211/15 "2022-02-11T16:21:15Z")

</div>

Wow. I hadn’t come across this one `[1.0; 2.0; 3.0 -4.0]` yet, I’ll keep it in mind!

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [February 11, 2022, 7:16pm UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211/16 "2022-02-11T19:16:10Z")

</div>

> [@fgerick](#):
>
> `julia> [1.0 2.0 3.0 - 4.0]`

Ouch. Would anyone miss it if this were disallowed (note, doesn’t strictly have to disallow for `Vector`s, only for 1×N matrices, but probably best to do for all cases for consistency)? It doesn’t seem super useful, and only allowing:

```julia
julia> [1.0 2.0 (3.0 - 4.0)]

```

seems would be ok. In practice (for arrays) how often would you actually do something like this, on purpose, or at least where `a - b` are variables (or if either is not a literal)?

At least accident-waiting-to-happen (a bug?) is something a linter, could and should (do they) catch. If not considered a bug, will have to wait for Julia 2.0… until then, I’ve been thinking Julia needs a debug/warning mode, and if running in the REPL, it seems it could be on by default.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 11, 2022, 7:19pm UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211/17 "2022-02-11T19:19:56Z")

</div>

> [@Seif\_Shebl](#):
>
> Could you please elaborate why this is horrendous?

We wouldn’t be able to write the following Matrix of Any:

```julia
v = [sin (2,3)]

```

It would error as `[sin(2,3)]`

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [February 11, 2022, 7:26pm UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211/18 "2022-02-11T19:26:26Z")

</div>

Banning that doesn’t seem “horrendous”. 🙂 At least for sin.

---

<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:** [February 11, 2022, 8:12pm UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211/19 "2022-02-11T20:12:45Z")

</div>

There is no problem with arrays as they have their special syntax anyways as I said above and as mentioned also by @StefanKarpinski . The problem dates back to 2014 when they wanted to allow `@m (a, b)` to mean calling the macro `@m` on a tuple. In my opinion, they chose the easiest solution not the best solution in that case. Functions are used more than anything else in Julia, but macros should only be second to them. So, I imagine the better solution was to choose (and I can’t assess how difficult this is TBH):

```julia
@m a, b # call @m on two variables a, b
@m a b # call @m on two variables a, b
@m (a, b) # call @m on single variable (a, b)

```

and leave functions alone. That’s, force a space after macro names because macros are equivalent to functions without the parentheses; `f(x, y)` should be equivalent to `@f x, y`.  
Allowing spaces after function names, however, can significantly improve readability in many cases. the [GNU Coding Standards](http://www.gnu.org/prep/standards/html_node/Formatting.html) recommends this (“We find it easier to read a program when it has spaces before the open-parentheses and after the commas”). A similar case in Julia is when they forced a space before `?` and after `:` in `1 > 2 ? 1 : 0`. In this case, `1 > 2? 1 : 0` is like normal English and is not bad at all. Many people also, including myself, have the habit of adding that space before function names for clarity (I use it a lot in my introductory C course). In general, languages that give freedom and trust to the programmer seem to be more enjoyable (Nim was criticized at some point for relying heavily on spaces). Some emerging languages even went farther and removed macros altogether, but I think macros are pretty useful though.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 11, 2022, 8:52pm UTC](https://discourse.julialang.org/t/whitespace-sensitivity/76211/20 "2022-02-11T20:52:46Z")

</div>

> [@Seif\_Shebl](#):
>
> the [GNU Coding Standards](http://www.gnu.org/prep/standards/html_node/Formatting.html)

If you’re writing Lisp, sure. I mean in that case, (f (2, 3))means f((2, 3)) right? Postfix, I would add space in Lisp too because otherwise I would think I’m calling the function with two arguments

[Next page](https://discourse.julialang.org/t/whitespace-sensitivity/76211.md?page=2)
