# Why I can't set what parameters are mutable explicitly?

**URL:** <https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091>\
**Category:** Offtopic\
**Tags:** functions\
**Created:** [August 21, 2022, 3:50pm UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091 "2022-08-21T15:50:10Z")\
**Posts on this page:** 20\
**Page:** 8

<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:** [August 23, 2022, 2:54am UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091/143 "2022-08-23T02:54:06Z")

</div>

> [@Dan\_Micsa](#):
>
> I don’t want it soon.

By soon I don’t mean when the feature might arrive, I was talking about the `const` check occuring long before a function call, which would have to be Julia’s parse time.

> [@Dan\_Micsa](#):
>
> I just want to point that this lack of feature will stop Julia to become a big language used for big projects.

Python is one of the biggest languages out there now and it doesn’t have this kind of `const`. The Final you mentioned earlier is a feature of mypy, and it’s more like Julia’s `const` than C++'s `const` because Python variables are very similar to Julia variables in design. There are many reasons people elect not to use Julia more, and while I’ve heard about insufficient compilation saving on a regular basis, this is the first time I’ve heard about `const`ing mutable instances via a variable. It’s fine to use statically typed languages like C++ (maybe Rust is a better analog because it’s also based on LLVM) and their features to develop large projects, but I really don’t see other people demanding such features from a dynamically typed language like Julia. It’s a contradictory demand like wanting a heavy sprinting bird to also fly well.

> [@Dan\_Micsa](#):
>
> I’m glad that we arrive to the conclusion that constness of parameters should be used to generate the unique name in the compiler to properly support this step like it is done in other languages used in large projects.

I’m pretty certain that the near-consensus here was that Julia can’t support this particular feature, though a linter could do other useful static analyses at parse-time, like recognizing the `x`/`x!` typo earlier.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [August 23, 2022, 3:04am UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091/144 "2022-08-23T03:04:22Z")

</div>

> [@Benny](#):
>
> ’m pretty certain that the near-consensus here was that Julia can’t support this particular feature, though a linter could do other useful static analyses.

And when we say can’t we mean provably it’s impossible it’s equally hard as solving the general halting problem in fact it can be converted into a halting problem.

It’s easy to construct ridiculous examples that illustrate this for example create a function which reads continuously from the cryptographic random number generator on the operating system and if it encounters the text of Romeo and Juliet it writes out a Julia expression that creates a method for a function which mutates its arguments which is a function that you’re const method calls.

People want the dynamic capacity and that’s what Julia is about this dynamism hence mathematically we can show the problem of determining constness is unsolvable QED

---

<div class="post-metadata">

**Author:** ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)\
**Post date:** [August 23, 2022, 8:24am UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091/145 "2022-08-23T08:24:53Z")

</div>

„determining constness” is trivial.

f(mut a, b) has the b constant. You don’t need any fancy examples or tools to determin it.  
The only minor change to current Julia is to treat as different functions f(mut a, b) and f(a, b).

We are amasing at thinking complicate even when the problem is obvious.

QED (?) 😄

---

<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:** [August 23, 2022, 8:45am UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091/146 "2022-08-23T08:45:15Z")

</div>

> [@Dan\_Micsa](#):
>
> f(mut a, b) and f(a, b)

This is not clear to me, still, and I’m wondering if you are referring to mutability of values (which you can have in Julia) or mutability of variables (which you can’t).

So, if I define

```julia
f(mut a::Vector) = ...
f(a::Vector) = ...

```

and then I call `f([1, 2, 3])`, which version of `f` should be invoked?

---

<div class="post-metadata">

**Author:** ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)\
**Post date:** [August 23, 2022, 9:12am UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091/147 "2022-08-23T09:12:45Z")

</div>

> [@DNF](#):
>
> This is not clear to me

I explained before how is done in C++ because I’m more familiar with it. It is called function overloading resolution or yet another level of static or compile time polymorphism. Operator overloading lhs and rhs in C++ is done like this for example:  
When addressing the elements of a vector in const manner (rhs) or a = v[i] it uses the constat version of it and in the v[i] = a it uses lhs version. So even the operator has two different implementations. Same with any other call in C++ void f(int&) is not the same with void f(const int&).

```julia
struct T
{
   value_t& operator[](std::size_t idx) { return mVector[idx]; } //lhs or mutable one.
   const value_t& operator[](std::size_t idx) const { return mVector[idx]; } //rhs or const version.
};

```

---

<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:** [August 23, 2022, 9:27am UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091/148 "2022-08-23T09:27:00Z")

</div>

But my question is rather:

```julia
f(mut a::Vector) = (a[1] = 2*a[2])
f(a::Vector) = sum(a)

```

What should happen when I write `f([1, 2, 3])`? Should the first or second version of `f` be invoked?

---

<div class="post-metadata">

**Author:** ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)\
**Post date:** [August 23, 2022, 9:38am UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091/149 "2022-08-23T09:38:07Z")

</div>

good question!

It will call the safest version or the second one in your example.

Example:

```julia
void ff(int &) { cout << "1\n"; }
void ff(const int &) { cout << "2\n"; }

int main()
{
	int n1{0};
	const int n2{0};
	ff(n1);
	ff(n2);
}

```

Output:  
1  
2

---

<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:** [August 23, 2022, 9:39am UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091/150 "2022-08-23T09:39:06Z")

</div>

And if I want to call the first one, should I do something like `f(mut [1, 2, 3])`?

---

<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:** [August 23, 2022, 9:43am UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091/151 "2022-08-23T09:43:37Z")

</div>

To me that seems quite similar to something like this:

```julia
f(a::Mutable{Vector}) = (a[1] = 2*a[2])
f(a::Vector) = sum(a)

```

and calling `f([1, 2, 3])` or `f(Mutable([1, 2, 3]))` to select method.

This is assuming that normal vectors have been made an immutable type.

---

<div class="post-metadata">

**Author:** ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)\
**Post date:** [August 23, 2022, 9:46am UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091/152 "2022-08-23T09:46:58Z")

</div>

yes in C++ you can const\_cast if you are in this case. But is very rare to do that. I use it maybe 2…3 times in my life. Usualy I have to cast away constness in flags in suposidly const objects when is expected a const behaviour but some unimportant things needs to be flagged. This are marginal uses.

---

<div class="post-metadata">

**Author:** ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)\
**Post date:** [August 23, 2022, 9:49am UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091/153 "2022-08-23T09:49:20Z")

</div>

In C++ is about parameters in general not only vectors.

---

<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:** [August 23, 2022, 10:01am UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091/154 "2022-08-23T10:01:41Z")

</div>

But Julia functions don’t know anything about parameters or variables. They just see the values passed to them, and those values are either mutable or immutable. So the only avenue is to change the mutability state of the value. So if you want

```julia
foo(const a::MyType) = ...
foo(a::MyType) = ...

```

that would just be syntactic sugar for something like

```julia
foo(a::Const{MyType}) = ...
foo(a::MyType) = ...

```

and then you can select method by setting `a` to either point to a `const` or non-`const` value. This cannot be encoded into the variable `a` itself, separately from the value it points to, due to Julia being dynamically typed.

Isn’t the core issue here that you want Julia to be a fully statically typed language?

---

<div class="post-metadata">

**Author:** ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)\
**Post date:** [August 23, 2022, 10:11am UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091/155 "2022-08-23T10:11:56Z")

</div>

> [@DNF](#):
>
> But Julia functions don’t know anything about parameters or variables.

Julia is like many other languages, the parameters that you pass have a type and that type is used to specialise various implementations of a function. Julia is like C++ at compile time.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/b/db78e9e9ae2d12bacccd8cf5646c266ddc3ccaed.png)

running this in Julia will make you understand better this:

```julia
f(a::Int64, b::Float64) = print(1)
f(a::Float64, b::Int64) = print(2)

a=1
b=1.

f(a, b)
f(b, a)

```

will print: 1, 2

---

<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:** [August 23, 2022, 10:40am UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091/156 "2022-08-23T10:40:04Z")

</div>

> [@Dan\_Micsa](#):
>
> the parameters that you pass have a type

But that’s the thing: they don’t. The parameters have no type, only the values do. That’s the main difference between static and dynamic typing. I increasingly get the impression that this is a root of the confusion here: Variables do not have types, values have types, unlike in static languages, where variables do have type.

Are you not applying the static model to a dynamic language in this discussion?

---

<div class="post-metadata">

**Author:** ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)\
**Post date:** [August 23, 2022, 10:47am UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091/157 "2022-08-23T10:47:02Z")

</div>

Indeed they don’t but 100% of the time variables that you declare have a type. You should learn a bit at least about the types in Julia before writing. They are very cool.

![image](https://global.discourse-cdn.com/julialang/original/3X/0/c/0c32a433583fc0562f37add4b14c2bfdf4755e23.png)

---

<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:** [August 23, 2022, 10:49am UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091/158 "2022-08-23T10:49:25Z")

</div>

That’s not the type of the variable `a`, it is the type of the value that `a` refers to.

I have been using Julia since 2014, or so, so I would appreciate it if you take a slightly less condescending tone (I have seen this through the entire thread, btw, not just now.)

---

<div class="post-metadata">

**Author:** ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)\
**Post date:** [August 23, 2022, 10:58am UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091/159 "2022-08-23T10:58:29Z")

</div>

At any given moment in time a declared variable has a type. If it has a type or reffers too is the same thing. Maybe you want to say that sometimes is a reference to a storage that store a type is correct too. But for simple types is NOT so! Simple types are passed by value so is not a “reffers to” but is the value.

Just use the suggestive called: typeof() on any value and you’ll see the current type.

---

<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:** [August 23, 2022, 11:05am UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091/160 "2022-08-23T11:05:49Z")

</div>

No. This is (at least one of) the main difference between static and dynamic languages: that in static languages the variables have types, in dynamic ones they do not. This is apparently a deep (but understandable) confusion that you hold, due to having primarily experience with static typing. Until it is cleared up, it will be hard to get any further.

> [@Dan\_Micsa](#):
>
> If it has a type or reffers too is the same thing.

No.

> [@Dan\_Micsa](#):
>
> But for simple types is NOT so! Simple types are passed by value so is not a “reffers to” but is the value.

No, all arguments are passed by sharing ([Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/#Argument-Passing-Behavior)). There is no difference in how they are passed. The only difference is that some values are mutable and some immutable. They are all passed according to the same semantics.

> [@Dan\_Micsa](#):
>
> Just use the suggestive called: typeof() on any value and you’ll see the current type.

Yes, the type of the _value_, not of the variable.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [August 23, 2022, 11:12am UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091/161 "2022-08-23T11:12:33Z")

</div>

I haven’t read this long thread, but I do agree that C++ does `const` a lot better than Julia’s `mutable struct` vs `struct`. `const` also transitively applying to the fields of a struct and to pointers is great.  
It’s silly that `MArray` and `SArray` should be two separate types in Julia IMO.

Rust does it even better. From this call site, can you guess which is a mutable borrow, which is immutable, and which is passed by value (which gives ownership in Rust, invalidating it in this scope):  
`foo(&mut x, &y, z)`  
?

---

<div class="post-metadata">

**Author:** ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)\
**Post date:** [August 23, 2022, 11:43am UTC](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091/162 "2022-08-23T11:43:25Z")

</div>

> [@DNF](#):
>
> No. This is (at least one of) the main difference between static and dynamic languages

I think you are a bit confused by the “dynamic” buzzed word. With this powerful exprimation C is a “dynamic” language because has void\*, macros and unions exactly how C++ is a “dynamic” language because it has void\*, unions, templates, macros and any type. However all 3 languages: C, C++ and Julia at each moment in time have the declared variables having a type! That type at the momment of invocation is used to proper specialise functions.

It is very true that for simple types (Int, Float) the variable “stores” the data or refers or points to an individual place in memory and in the case of large objects containers, strings, tuples it stores a reference to same data. This is clear not relevant to the fact that ALWAYS any variable in Julia at any moment in time points to a well known type.

btw you can write how many NO you want I gave you plenty of examples to exemplify this. Please instead of NO give an example when the declared variable has no type. Obviously declared is declared not used as a template in a generic definition of a function. Don’t try with Nothing because is a known type.

[Previous page](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091.md?page=7)

[Next page](https://discourse.julialang.org/t/why-i-cant-set-what-parameters-are-mutable-explicitly/86091.md?page=9)
