In Julia, this is actually more consistent than in C++, although I think 
it's more by chance than by design.

Since `a * b` expands to `*(a, b)`, *all* operands of the multiplication 
will be evaluated before the multiplication operation, regardless of their 
order. Thus, if evaluating `b´ modifies `a`, then `a` will be passed 
modified into the multiplication method. Demo:

```
julia> type Foo x end
julia> import Base: *
julia> *(x::Foo, y::Foo) = x.x * y.x
* (generic function with 287 methods)
julia> f!(foo) = (foo.x *= 2; foo)
f! (generic function with 1 method)
julia> a = Foo(1); f!(a) * a
4
julia> a = Foo(1); a * f!(a)
4
```

In C++, on the other hand, `*` is an operator *member* of the first 
operand, which leads to the different behavior.

// T

On Friday, September 11, 2015 at 3:24:54 PM UTC+2, Yichao Yu wrote:
>
> On Fri, Sep 11, 2015 at 3:44 AM,  <[email protected] <javascript:>> 
> wrote: 
> > This question on Stackoverflow 
> > 
> http://stackoverflow.com/questions/32504524/is-value-of-xfx-unspecified-if-f-modifies-x
>  
> > was asked for C++ recently. I think it makes sense to ask it for Julia, 
> too, 
> > given its pass-by-sharing and we-are-all-consenting-adults semantics, 
> and I 
> > was wondering what the answer is. Of course I can test it on an example, 
> but 
> > I was wondering if this detail is defined explicitly in the 
> > specifications/docs. 
>
> In julia, every objects are passed by reference so I don't think 
> this would be a problem. The compiler will not make a copy of an 
> object and pass it as the argument unless it is an immutable (in which 
> case f(x) cannot modify x). 
>
> > 
> > Thanks! 
> > 
>

Reply via email to