Why is Julia set immutability by type (and not variable)?

In Julia, why is immutability a “property” of general types (in Julia is a sense of the word , therefore more like a structure or class in other languages) and not variables?

eg. In Rust (and I think most languages ​​with immutability support), whether something is immutable is set for certain variables, and not for general types, i.e. No separate structures Vectorvs ImmutableVector. To create an immutable vector, I do let v = Vec::new(). To create changeable, I do let mut v = Vec::new(). mutis a keyword that can be applied to any structure.

This seems more convenient, because you can do something immutable, and variables are immutable by default (that Julia’s people want people to do as much as possible [1]). Are there pragmatic or performance improvements with the Julia approach?

1: https://github.com/JuliaLang/julia/issues/13#issuecomment-11007166

+4
source share
2 answers

I think this is pretty much a semantic difference. Although this is somewhat less convenient than Rust syntax, if you really want to, you can do the same in Julia with abstracttypes:

abstract MyType
type MyTypeMut <: MyType
    a::Int
end
immutable MyTypeImmut <: MyType
    a::Int
end

Mut/Immut, , , MyType.

+1

- ,

, - , .

:

let v = Vec::new().
let mut v = Vec::new()

, , , "Vec", - . "mut" .

, :

let v = Vec<Pure>::new().
let v = Vec<Mutable>::new().

mutable/immutable. , , .

"mut" - - "". , . .

, , mutability/effects , - "", . , "mut" , - , , .

+5

All Articles