On Monday 07 September 2015 13:12, Bahman Movaqar wrote: > I was under the impression that when a variable, that is declared > as `immutable`, is passed to a function, a copy of the value is > passed. > > However based on "marks" I can imagine that since the data is > marked as `immutable` only a reference is passed; and the > compiler guarantees that what is referenced to never changes. Am > I right?
Generally immutable doesn't affect how things are passed around. An immutable type is passed the same as a mutable version of it. Compilers may optimize based on immutability. This may mean that a reference is passed instead of a copy, or the other way around. I don't know if compilers do such things currently, or how much potential is in that. Also, I don't think the language has a stance on that; it's purely an optimization. immutable does affect how you can pass things around, though. An example: ---- void f(int a) {} void g(int* a) {} void main() { int xm; immutable int xi; f(xm); /* ok, obviously */ f(xi); /* ok */ int* ym = &xm; immutable int* yi = ξ g(ym); /* ok, obviously */ g(yi); /* doesn't compile */ } ---- f(xi) is ok because when passing an int or an immutable(int), it's copied completely. So the f cannot possibly mutate the original immutable variable xi. g(yi) is not ok, because when passing an int* or an immutable(int*), only the pointer is copied. So g could dereference yi and mutate the referenced immutable variable xi. The same applies to other forms of references: classes, ref parameters, arrays.