Juno, Julia, and indentation


#1

I have mainly used Jupyter Notebook for playing around with Julia. I’ve seen a couple of references on how to install Jupyter Lab, but haven’t figured out how to do that. In the mean time, I also use Juno occasionally.

I seem to have read that indentation is not needed in Julia, while in Python this is essential. By this, I mean indentation in function blocks, for loop blocks, if statement blocks, etc. But that indentation is recommended for readability. I also seem to have read somewhere that for packages, one should not use indentation.

When I use Juno, the code doesn’t run unless I have indentation. Questions:

  • Have I mistaken the indentation rules in Julia?
  • If not, why does Juno behave as it does?

Another thing: in MATLAB, line continuation is signaled by ellipsis ("…"), while in Julia, line breaks are supposed to be ok as long as the last character “signals” that the statement is incomplete. In other words, if a line ends with unbalanced parenthesis, that is a signal that the statement continues on the next line. Ending with an operator, e.g., “+”, is a signal that the statement continues on the next line.

I tend to get problems with this in Juno, and it seems like I’m forced to use very long lines and avoid line breaks.

  • Is my understanding of line breaks correct?
  • If so, why do I get problems in Juno?
  • If I use line breaks, is it permittable to use tabulator for the next line so that commands are lined up? In other words:
                x = A*x +
                      b

is this allowed? (I’m assuming that the first line is indented in some function block, some for loop, etc.). Or do I have to type:

                x = A*x +
b

?


#2

I think what you mean is that you shouldn’t intend the code that is between module and its corresponding end. This is just because otherwise the whole code in that file would be indented which is unnecessary.

This is true, for exmaple

             x = A*x +
                      b

is fine

julia> A = rand(2,2); x = rand(2); b = rand(2);

julia> x = A*x +
             b
2-element Array{Float64,1}:
 1.06627738851452  
 0.7755762881638293

There seems to be some Juno problem here. I think Juno decides what is considered a block of code based on indentation so that might be what is happening?


#3

Thanks for clarification. So the problem may be due to Juno. I think Jupyter handles this without problems.


#4

If you mark it in Juno and then execute the marked code (I think there is such a keyboard shortcut) it should work ok.


#5

Thanks, will try tomorrow.

Btw… hovering the cursor on top of the left side margin icons in Juno reveals the shortcuts. The keyboard shortcut for running a block of marked code is “Ctrl+Return”. Which is the same shortcut as running a simple line.

The shortcut description is messy, though: Juno on Windows 10 has shortcut descriptions that are a mix of Mac syntax and Win syntax. As an example, the keyboard shortcut for running a file is described as “Shift+Cmd+Return”… which probably should have been “Shift+Ctrl+Return”; anyway, “Shift+Ctrl+Return” executes the file.


#6

In Jupyter, you typically run a whole cell, and can’t run things line by line. You can do this in Juno also - there are ways to mark up “cells” in your code and execute them, or you can select all the code you want to run and do cmd/Ctrl enter. If you just have a cursor ( no text selected), Juno will try to identify the smallest unit of code it can execute, but it mostly does this using tabs as a heuristic.

Note: inner tabs don’t matter, if you have a function definition for example, as long as there’s an indent for everything after the function line, none of the inner loops or conditionals need additional indents, the whole block will be executed.

This doesn’t always work perfectly, but it’s pretty good. And I actually sometimes use it to my advantage - for example if I have a plot call and then I want to save the figure, I’ll just indent thesavefig() call too and Juno will execute it as if it’s part of the same block.


#7

Another weird behavior in Juno: inserting a block comment bracketed by #= and =# is ignored… Juno interprets "= as a comment, “faithfully” runs all the code in between brackets (which was meant to be commented out), and flags an error on =#.