# Indentation and the end statement

**URL:** <https://discourse.julialang.org/t/indentation-and-the-end-statement/74076>\
**Category:** New to Julia\
**Created:** [January 5, 2022, 9:05am UTC](https://discourse.julialang.org/t/indentation-and-the-end-statement/74076 "2022-01-05T09:05:34Z")\
**Posts on this page:** 18\
**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 14, 2022, 7:02am UTC](https://discourse.julialang.org/t/indentation-and-the-end-statement/74076/22 "2022-01-14T07:02:10Z")

</div>

I hope we never share a project, that looks very annoying.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [January 14, 2022, 7:45am UTC](https://discourse.julialang.org/t/indentation-and-the-end-statement/74076/23 "2022-01-14T07:45:29Z")

</div>

Julia does have string macros, so in theory, anybody can write a package that implements any syntax they want. For example, the Julia parser would accept the following without complaining:

```julia
using indentation
indented"""
for i in 1:10
   if i < 5
      print(i)
   else
      print(i*i)

function f(x,y)
   return(x+y) 
"""

```

For this to work, the “indentation” package should contain a macro named `indented_str`, which would parse the indented code (perhaps using a modified version of CSTParser.jl) and return the resulting expression.

This would be very easy to do, (but even if somebody did it, I doubt that the package would get much use.)

---

<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:** [January 14, 2022, 3:26pm UTC](https://discourse.julialang.org/t/indentation-and-the-end-statement/74076/24 "2022-01-14T15:26:57Z")

</div>

A+ trolling

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [January 14, 2022, 5:44pm UTC](https://discourse.julialang.org/t/indentation-and-the-end-statement/74076/25 "2022-01-14T17:44:18Z")

</div>

I didn’t even notice the missing `.` till you pointed it out, and `.` aren’t even redundant with good indentation like `end` is (often). Our viscera react so differently.

---

<div class="post-metadata">

**Author:** ![portig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/portig/32/5753_2.png) [@portig](https://discourse.julialang.org/u/portig)\
**Post date:** [January 16, 2022, 12:43am UTC](https://discourse.julialang.org/t/indentation-and-the-end-statement/74076/26 "2022-01-16T00:43:29Z")

</div>

I have deleted my offending post. I was definitely NOT intending to troll this discussion. I misinterpreted what kind of comments would be appropriate here. Sorry!

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 16, 2022, 1:12am UTC](https://discourse.julialang.org/t/indentation-and-the-end-statement/74076/27 "2022-01-16T01:12:31Z")

</div>

Disclaimer: I really doubt what I’m about to show is best practice, but I almost never need to do this anyway.

In PEP 572, Python’s walrus operator is introduced, and one of the justifications was to enable an assignment in an if/elif line with a conditional operation so one didn’t have to indent for each condition, like so:

```julia
match1 = pattern1.match(data)
if match1:
    result = match1.group(1)
else:
    match2 = pattern2.match(data)
    if match2:
        result = match2.group(2)
    else:
        result = None

# could be replaced with- elif (match2 := pattern2.match(data))

```

Incidentally, Julia’s assignments can also be used like a walrus operator, but both only work in one-liners. If I needed more lines to calculate `match2`, I either have to indent or refactor the lines as a separate function, which may be too small for me to think it’s worth separating. Since Julia doesn’t force indentation, I can just move all the `result`-calculating sections to the same indentation column.:

```julia
match1 = match(pattern1, data)
if match1
    result = group(match1, 1)
else
# a few more lines here
if ( match2 = match(pattern2, data) )
    result = group(match2, 2)
else
    result = nothing
end end

```

Obviously the multiple trailing `end`s are annoying, but it unambiguously expresses where the outermost if-else statement starts and ends, which is made necessary by how I tampered with indentation.

---

<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:** [January 16, 2022, 2:12am UTC](https://discourse.julialang.org/t/indentation-and-the-end-statement/74076/28 "2022-01-16T02:12:18Z")

</div>

I wasn’t offended! On the contrary, I thought it was excellent and humorous trolling.

---

<div class="post-metadata">

**Author:** ![sashmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sashmit/32/18791_2.png) [@sashmit](https://discourse.julialang.org/u/sashmit)\
**Post date:** [January 16, 2022, 2:37am UTC](https://discourse.julialang.org/t/indentation-and-the-end-statement/74076/29 "2022-01-16T02:37:51Z")

</div>

To me another approach could be a julia equivalent to parinfer for various lisps:

> **[Parinfer - simpler Lisp editing](https://shaunlebron.github.io/parinfer/)**
>
> Parinfer - simpler Lisp editing

but with autoinserting nested ends instead of ) if the indentation was right.

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [January 21, 2022, 2:24am UTC](https://discourse.julialang.org/t/indentation-and-the-end-statement/74076/30 "2022-01-21T02:24:41Z")

</div>

I don’t think that code actually works? Julia doesn’t really have “truthy” values (usually a good thing, though a bit less concise in this case) so you can’t use a regex match in a conditional. However, nearly everything in Julia is an expression, so you can just use a block with semicolon in the conditional as follows:

```julia
julia> str = "baaac"
       if (m = match(r"(a+)", str); !isnothing(m))
           @info "Matched first clause" m[1]
       elseif (m = match(r"(c+)", str); !isnothing(m))
           @info "Matched second clause" m[1]
       end
┌ Info: Matched first clause
└ m[1] = "aaa"

```

This isn’t exactly pretty, but is a fairly concise way to express this kind of pattern.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 21, 2022, 2:01pm UTC](https://discourse.julialang.org/t/indentation-and-the-end-statement/74076/31 "2022-01-21T14:01:01Z")

</div>

Right, I should’ve specified it’s just a rough Julia-looking pseudocode mirroring the Python snippet from PEP 572.  
That is actually a really good tip. I could write multiple lines that way, and I could also write it as a begin block, which would save me the trouble of closing the parentheses and adding semicolons. For immediate visual clarity, I could indent the condition code differently from the branch code. Don’t have to worry about counting trailing ends anymore.

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [January 23, 2022, 5:03am UTC](https://discourse.julialang.org/t/indentation-and-the-end-statement/74076/32 "2022-01-23T05:03:09Z")

</div>

> [@Benny](#):
>
> I could also write it as a begin block, which would save me the trouble of closing the parentheses and adding semicolons

Yes exactly. Formatting those `begin ... end` blocks in a readable way might be challenging but there’s a very neat consistency in embracing “everything is an expression”.

---

<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:** [March 30, 2022, 9:44am UTC](https://discourse.julialang.org/t/indentation-and-the-end-statement/74076/33 "2022-03-30T09:44:54Z")

</div>

> [@DNF](#):
>
> The whitespace sensitivity of Python is imo its single worst feature

I didn’t care about this until I had to debug a piece of python code that had a wrongly indented return statement. It took me over half a day to spot.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [March 30, 2022, 11:49am UTC](https://discourse.julialang.org/t/indentation-and-the-end-statement/74076/34 "2022-03-30T11:49:42Z")

</div>

Ideally the editor should highlight such issues, making the process simpler.

---

<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:** [March 30, 2022, 12:42pm UTC](https://discourse.julialang.org/t/indentation-and-the-end-statement/74076/35 "2022-03-30T12:42:32Z")

</div>

That might not be possible, if the indentation change alters the meaning of the code, without causing an error.

---

<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:** [March 30, 2022, 1:01pm UTC](https://discourse.julialang.org/t/indentation-and-the-end-statement/74076/36 "2022-03-30T13:01:25Z")

</div>

It was something along these lines:

```python
def f(n):
  s = 0
  for i in range(n):
    s += 1
    return s

```

After that I have started to add

```python
  #endfor

```

everywhere 🙂

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [March 30, 2022, 1:12pm UTC](https://discourse.julialang.org/t/indentation-and-the-end-statement/74076/37 "2022-03-30T13:12:16Z")

</div>

We’ve had problems with python going throught Git and coming out the otherside with messed up indentations

Python itself will often simply not compile your script.

Sure, VSCode will also tell you which lines are badly indented.

but two people mixing tabs and spaces on the same line, everything working on one of their merges

and suddenly - boom - indent chaos

“you should have done something different before you got to that point”

yes, choose a langage with braces / end

---

<div class="post-metadata">

**Author:** ![paulmelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulmelis/32/35063_2.png) [@paulmelis](https://discourse.julialang.org/u/paulmelis)\
**Post date:** [March 30, 2022, 1:27pm UTC](https://discourse.julialang.org/t/indentation-and-the-end-statement/74076/38 "2022-03-30T13:27:24Z")

</div>

> [@lawless-m](#):
>
> yes, choose a langage with braces / end

Or install a git commit hook that disallows any .py files that use tabs for indenting (or at the very least disallow files mixing spaces and tabs).

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [March 30, 2022, 1:34pm UTC](https://discourse.julialang.org/t/indentation-and-the-end-statement/74076/39 "2022-03-30T13:34:08Z")

</div>

# apt install time-machine

[Previous page](https://discourse.julialang.org/t/indentation-and-the-end-statement/74076.md?page=1)
