Is it safe to make a module inside another using the same local characters?

It is sometimes useful for me to use a local module (inside the parent module) with its own local characters, which does a small task for use only by the parent module. This is useful when a module becomes large, and there is no good reason for making smaller helper functions outside this module, since these helper functions are really necessary and are used by only one parent module.

Here is a silly example with one module and an auxiliary module inside it, something

foo[x_] := Module[{r}, r = Module[{y, sol}, sol = First@Solve [y^2 + 3 y + 2 == 0]; y /. sol ]; x^r ] 

But the problem is that local variables for the internal module can conflict with local variables with the parent module, because the M-notebook turns the internal local variables of the module red when I do the following, which tells me: I do what I donโ€™t should do: (I don't want to bother constantly, checking if I use a unique local character for the internal module, which is different from the one used as local characters for the parent module, in the end it should be loc and also have a different symbol name with you if this is so)

 foo[x_] := Module[{r, y=0}, r = Module[{y, sol}, sol = First@Solve [y^2 + 3 y + 2 == 0]; y /. sol ]; x^r ] 

Pay attention to the red coloring, which, according to help, is a local conflict or shading of the area in several contexts.

enter image description here

(M need to use the best colors, it is difficult to make the difference between many colors, all shades of red).

enter image description here

(I think this is shadowingg warning). In any case, this tells me that I should not do this, although I did not see any problems with this design when I used it.

The value of the parent module, the local variable 'y' in this example, was not overwritten by a call to the internal module 'r', which is good.

Again, I did not want to make the function outside of foo, because this small task is used only by foo [], and there is no need to move it to the global context.

Of course, I could always just write:

 foo[x_] := Module[{r, y, sol}, sol = First@Solve [y^2 + 3 y + 2 == 0]; r = y /. sol; x^r ] 

But I just give an example, and this is for a large module, where it helps to break down tasks inside the module into several smaller tasks. Internal functions, internal functions - this is what I used earlier in other languages, such as Ada / Pascal, and those that have this design and can be useful for large programs.

My question is: I just want to check with experts here if it is safe for me to use the above, although M gives me this red warning about coloring? and if there is something that I need to worry about it

thanks,

+4
source share
2 answers

Yes, it is safe to use the same variable in the nested Modules if you do not lose them. Mathematica treats each variable defined in Module as local to that module. Local variables are Temporary and are numbered as variable$ModuleNumber . You can verify this for yourself in the following example:

 Module[{x = 1}, Print[ HoldForm@x , " = ", x] Module[{x = 2}, Print[ HoldForm@x , " = ", x] Module[{x = 3}, Print[ HoldForm@x , " = ", x] ]; Print[ HoldForm@x , " = ", x] ]; Print[ HoldForm@x , " = ", x] ] (*Output x$4576 = 1 x$4577 = 2 x$4578 = 3 x$4577 = 2 x$4576 = 1 *) 
+8
source

As far as I know, this is a small detection problem.

+2
source

Source: https://habr.com/ru/post/1387518/


All Articles