<p dir="auto">I'm trying to define a type that can be added/multiplied with floats.<br>
I've tried overloading <code class="notranslate">+</code> like this:</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="pub struct Num(f64);

impl Add&lt;Num,Num&gt; for f64 {
    fn add(self, other: Num) -&gt; Num {
        match other {
            Num(x) =&gt; Num(self + x)
        }
    }
}"><pre class="notranslate"><span class="pl-k">pub</span> <span class="pl-k">struct</span> <span class="pl-smi">Num</span><span class="pl-kos">(</span><span class="pl-smi">f64</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-k">impl</span> <span class="pl-smi">Add</span><span class="pl-kos">&lt;</span><span class="pl-smi">Num</span><span class="pl-kos">,</span><span class="pl-smi">Num</span><span class="pl-kos">&gt;</span> <span class="pl-k">for</span> <span class="pl-smi">f64</span> <span class="pl-kos">{</span>
    <span class="pl-k">fn</span> <span class="pl-en">add</span><span class="pl-kos">(</span><span class="pl-smi">self</span><span class="pl-kos">,</span> <span class="pl-s1">other</span><span class="pl-kos">:</span> <span class="pl-smi">Num</span><span class="pl-kos">)</span> -&gt; <span class="pl-smi">Num</span> <span class="pl-kos">{</span>
        <span class="pl-k">match</span> other <span class="pl-kos">{</span>
            <span class="pl-v">Num</span><span class="pl-kos">(</span>x<span class="pl-kos">)</span> =&gt; <span class="pl-v">Num</span><span class="pl-kos">(</span><span class="pl-smi">self</span> + x<span class="pl-kos">)</span>
        <span class="pl-kos">}</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">But</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="let x = Num(42.0);
println!(&quot;{}&quot;, 1.0 + x);"><pre class="notranslate"><span class="pl-k">let</span> x = <span class="pl-v">Num</span><span class="pl-kos">(</span><span class="pl-c1">42.0</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-en">println</span><span class="pl-en">!</span><span class="pl-kos">(</span><span class="pl-s">"{}"</span>, <span class="pl-c1">1.0</span> + x<span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">fails with the following error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="error: mismatched types: expected `_`, found `Num` (expected floating-point variable, found struct Num)
println!(&quot;{}&quot;, 1.0 + x);
                     ^"><pre class="notranslate"><code class="notranslate">error: mismatched types: expected `_`, found `Num` (expected floating-point variable, found struct Num)
println!("{}", 1.0 + x);
                     ^
</code></pre></div>
<p dir="auto">Interestingly,</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="let x: Num = Num(42.0);
println!(&quot;{}&quot;, 1.0.add(x));"><pre class="notranslate"><span class="pl-k">let</span> x<span class="pl-kos">:</span> <span class="pl-smi">Num</span> = <span class="pl-v">Num</span><span class="pl-kos">(</span><span class="pl-c1">42.0</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-en">println</span><span class="pl-en">!</span><span class="pl-kos">(</span><span class="pl-s">"{}"</span>, <span class="pl-c1">1.0</span>.add<span class="pl-kos">(</span>x<span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">works just fine.<br>
Is this intended behaviour?<br>
It seems inconsistent that <code class="notranslate">+</code> simply ignores the implementation of <code class="notranslate">Add</code> in this case.</p>
<h3 dir="auto">STR</h3>
<p dir="auto">Given the proper (commutative) <code class="notranslate">Add</code>/<code class="notranslate">Mul</code> implementations. (See this <a href="http://is.gd/HsB5yl" rel="nofollow">playpen link</a>)</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="const I: Complex&lt;f32&gt; = Complex { re: 0., im: 1. };

let z = I * 3.;  // OK
let z = I + 3.;  // OK
let z = 3. * I;  // Err
let z = 3. + I;  // Err"><pre class="notranslate"><span class="pl-k">const</span> <span class="pl-v">I</span><span class="pl-kos">:</span> <span class="pl-smi">Complex</span><span class="pl-kos">&lt;</span><span class="pl-smi">f32</span><span class="pl-kos">&gt;</span> = <span class="pl-smi">Complex</span> <span class="pl-kos">{</span> <span class="pl-c1">re</span><span class="pl-kos">:</span> <span class="pl-c1">0.</span><span class="pl-kos">,</span> <span class="pl-c1">im</span><span class="pl-kos">:</span> <span class="pl-c1">1.</span> <span class="pl-kos">}</span><span class="pl-kos">;</span>

<span class="pl-k">let</span> z = <span class="pl-v">I</span> <span class="pl-c1">*</span> <span class="pl-c1">3.</span><span class="pl-kos">;</span>  <span class="pl-c">// OK</span>
<span class="pl-k">let</span> z = <span class="pl-v">I</span> + <span class="pl-c1">3.</span><span class="pl-kos">;</span>  <span class="pl-c">// OK</span>
<span class="pl-k">let</span> z = <span class="pl-c1">3.</span> <span class="pl-c1">*</span> <span class="pl-v">I</span><span class="pl-kos">;</span>  <span class="pl-c">// Err</span>
<span class="pl-k">let</span> z = <span class="pl-c1">3.</span> + <span class="pl-v">I</span><span class="pl-kos">;</span>  <span class="pl-c">// Err</span></pre></div>
<h3 dir="auto">Output</h3>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="error: mismatched types: expected `_`, found `Complex&lt;f32&gt;` (expected floating-point variable, found struct Complex)"><pre class="notranslate">error<span class="pl-kos">:</span> mismatched types<span class="pl-kos">:</span> expected `_`<span class="pl-kos">,</span> found `<span class="pl-v">Complex</span>&lt;f32&gt;` <span class="pl-kos">(</span>expected floating-point variable<span class="pl-kos">,</span> found struct <span class="pl-v">Complex</span><span class="pl-kos">)</span></pre></div>
<p dir="auto"><a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/nikomatsakis/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/nikomatsakis">@nikomatsakis</a> Could this work in the future? It would be great if could type <code class="notranslate">3. + 4. * I</code> instead of <code class="notranslate">I * 4. + 3.</code>, the former feels more natural.</p>