Reading Julia documentation. 1. Reassigning to `pi`

I’m just started reading documentation of Julia and as a result I will write here some questions that are bogging me. I choose this category, because I would probably have many very basic questions.

In the section on variables we read that it is impossible to redefine a built-in constant or function that was already used. As such

pi
pi = 3

isn’t working in REPL. But is working in my Pluto notebook. Can you explain me why Pluto works in such way?

2 Likes

The short answer is that you can redefine constants within Pluto.jl (and thus struct as well).
This works by some magic that Pluto performs. Essentially every cell is its own namespace and when you use a variable in another cell then Pluto adds the respective namespace to the name behind the scenes. Thus when you “reassign” a constant, in reality Pluto creates a new namespace for that constant and replaces all references to the old namespace by the new namespace.

7 Likes

I do not recommend Pluto for learning Julia, as its disallows variable reassignment between cells while not being able to keep track of changes in mutable structs (including vectors). I recommend using a Jupyter Notebook. I recently had difficulties putting Julia to work in a Jupyter Notebook, so I understand the allure of using Pluto (which is fully written in Julia), but I was able to put it to work in my Arch Linux machine and wrote a step-by-step guide on how to make it work: Jupyter and Jupytext through IJulia.jl (Jupytext does not really work) - #4 by Henrique_Becker

4 Likes

Another gotcha moment for me. I now must rethink the use of Pluto.jl for educational purpose. My impact on Julia education is within statistical error, but I need to have some order in my head about such things.