As a Julia beginner/neophyte, I am wondering why using a ! name suffix for functions that may mutate their arguments is merely a convention, but is not enforced by the compiler?
It is nice to have this indication available when using language/package functions, but on the other hand I cannot rely on it (except maybe in the stdlib?), which I think somewhat diminishes its value.
At the same time, if this were strictly enforced, beside increasing clarity of the affected APIs, would that not also afford some avenues for internal optimisations by the compiler?
I’m sure this already came up before, but I searched both the docs and the forum and could not find a relevant discussion/information.
The only reasonable interpretation would be “no pointer derived from any argument is written to”.
Consider e.g., map(fun, arr) – what happens if fun has side-effects that write to memory? What if some methods of fun have side-effects and some don’t? Is this call violating a mutability contract? Do you want functions typed by purity?
In short: Neither Julia’s type system nor Julia conventions are a good fit for that kind of stuff. I guess haskell or idris might be languages where this makes more sense (either do some monadic dance or get a compiler error).
Btw, a variety of similar annotations make a lot of sense – on the method level, not the function level, and as hints to the optimizer (hints as in axioms / assumptions that the optimizer may trust). One of then is @pure, which is almost impossible to safely use, even for core developers (inofficial definition: “a method is @pure if and only if jameson says so”). Some others are available in llvm.
Also, the bang notation is not ideal, it does not indicate: which parameters risk being mutated, if some mutation always occurs or it depends on parameter values; and is, for example, ignored by all the IO functions in Base, because basically everything there would need a bang.
It is a great convention for distinguishing between two alternative functions (one which copy and the other that works inplace) and also as a danger sign for function that are often misused because the users assume they did not change the arguments. But I think trying to make it more than that would take a lot of effort which would not be worth.
Thank you for clearing this up for me, this all sounds reasonable! In a way this then seems similar in purpose to Python type annotations - optional, and not checked by the interpreter, mainly meant to ease the work of IDEs/linters/static analysers, and developers.
I’ll think on this some more…
It could very well be that, in practice, banning functions who’s name don’t end in ‘!’ from modifying their arguments is too difficult to be sensible - I don’t know enough about Julia to tell (I’m new too). But Tamas_Papp’s comment still made me want to express some thoughts.
I cannot rely on comments or (other) documentation being correct. I cannot rely on conventions being followed. I cannot rely on code being bug-free. But, barring bugs in the compiler, I can rely on rules of the language being followed. For instance, if a variable is declared const, I can rely on it not being modified. Or, if a procedure argument in Fortran is declared INTENT(IN), I can rely on it not being modified by the procedure. (In C, declaring a pointer argument to a function as const isn’t a guarantee, but gets you pretty close)
If I can declare what some code does or does not do, using language features rather than using comments or conventions, I strongly prefer that. Because comments can be wrong, and conventions can be broken, but language rules cannot. They can be relied upon. It’s nice not just for the user/reader of some code, but also for its writer, as it may catch bugs. What if I really didn’t intend to modify a function argument, but by mistake did anyway?
Obviously, there will always be uncertainty about what some unfamiliar code does, or whether your own code have bugs. But while less uncertainty isn’t as good as no uncertainty, it is still better than more uncertainty. Let’s not refuse to reduce uncertainty just because we can’t eliminate it.
If there was a way of declaring “INTENT” for function arguments in Julia, be it with exclamation marks at the end of function names or through some other means, I think it would be nice. Not something I can’t live without, and if it’s decided it isn’t worth the trouble, I can accept that, but still - it would be nice.
If someone really cares about this, it would be interesting to see a prototype of a linter that does whole program analysis to verify that a nominally non-mutating function never invokes any mutating functions in any given codebase.
In addition to Tamas point that you can misunderstand the documentation, I would like to point out that Julia patch versions exist exactly because the language failed to guarantee its own rules. I myself have already found an unreachable bug in Julia 1.0.5 and had to change versions because the correction would not be back-ported, so the only implementation of the language rules (for some major-minor version) may be broken and without prospect of repairs.
Oh? It seems I’ve learned something new today, then - thanks!
But let me point out that it still gives a warning. If you would declare that a is meant to be a constant only through some naming convention or a comment, you would get neither errors nor warnings when it’s modified.
It’s not an unimportant difference.
Thank you all for the interesting inputs and perspective. And especially @ErikE for formulating my intention in a much more eloquent way - I am more or less 100% aligned with what you say, about language features like INTENT, or const (at least the non-Julia one), or Modelica’s constant/parameter/variable variability indication, language features vs. convention vs. comments, etc. (btw, I was also surprised about the “direction” of the const-promise - thanks!)
I am wondering - what mechanism triggers these warnings when you modify a const object, and how well does that work (is there a limit to the detection heuristic)?
Can’t this same mechanism be adapted to warn if your function-without-bang mutates any of its arguments? After all, doesn’t a function-without-bang essentially correspond to having only const arguments (at least in the function’s scope)? Could that be leveraged somehow? E.g. could the compiler just slap consts in front of all function arguments if there is no bang at the end of the function name? (Surely it’s not as easy as that)
No–it really doesn’t work this way. The warning happens when you assign a new value to a variable which has been marked as const. That’s completely different than mutating an existing value, which is what the ! suffix refers to.
x has been mutated, but there is no warning, and the resulting code will work correctly with no issues. That’s exactly as intended: the const label has nothing to do with mutation of an existing value. Instead, the warning occurs if you assign a new value:
julia> x = [4, 5, 6]
WARNING: redefinition of constant x. This may fail, cause incorrect answers, or produce other errors.
No, definitely not, and for the same reason. A function with ! may mutate its arguments (which is not something const cares about).
As an exercise, consider:
x = [1, 2, 3]
x = 1
x = 2
x = 3
Both are perfectly legal functions, but only f2 will modify the value which was passed into it. f1 just creates a brand-new value with the same name in its local scope and has no effect on the value you pass in.