I forgot to mention that you can have a kind of in place update by using w[:] += 10
But this will currently still allocate a new temporary array, so the explicit loop will probably be faster. kl. 09:49:11 UTC+1 lørdag 25. januar 2014 skrev Ivar Nesje følgende: > > It is nice that you raise this issue. I think most of the others working > with Julia has experience from C or Python which also have the same > semantics. I'll make an attempt to sum up how things work in this case. > > 1. > `a += b` is just fancy syntax for `a = a + b`. > > If you look at code_lowered(), you can see that it actually expands to > `tmp = a+b; a = tmp` > > 2. > The meaning of `=` is dependent on what is on the left hand side: > * If LHS is just a variable, it means assignment `a = b` (mutable types > gives a reference) > * If LHS is a square bracket (indexing) expression, `a[4] = b` translates > to `setindex!(a,4,b)` (mutate the object) > * Julia also allows a function to be expressed with `=` when LHS is a > parenthesized expression, `a() = b` translates to `function a(); return b; > end` > > 3. > All operators (+,-,*,/) are just infix syntax for a function with the same > name. `a + b` is parsed as `+(a, b)`. When Julia evaluates a function, > there is no way for the function to know how the result will be assigned. > > Lots of functions that operate on arrays have separate mutating and > non-mutating functions, where the copy functions has the shortest and > easiest name. (eg sort vs sort!). You can use this for matrix > multiplication `A_mul_B!()`, but I do not know the status for the other > functions. > > Regards > Ivar > > kl. 07:17:32 UTC+1 lørdag 25. januar 2014 skrev Eric Ford følgende: >> >> So in the statements `w = x` and `w = x + 10`, the equals sign is doing >>> the same thing, it's just that in the latter case the + method is creating >>> a new array. >>> >> >> FWIW- I tried searching in the documentation to find some mention of + >> (or other operators) creating a new array. I couldn't find any >> documentation of this behavior. >> >> >>
