News for hackers. Stuff that matters.
Re:Rust Traits Deep Dive, Part 2 by Animats on Wednesday June 20, 2018 @03:27AM (#17353309)
The text, with examples, is here.[1] Read that, skip the audio.

About operators, he writes "So, to grab one of those earlier examples: when you write a + b, that's equivalent to calling a.add(b) or b.add(a) or even Add::add(a, b). The operator is special syntactical sugar for the trait." But which one of those?

This matters, because it leads to mixed-mode semantics trouble. Who determines the output type of "a + b"? "a"? "b"? Both? Are there implicit type conversions? This is where C++ gets into strange interactions between type converters and generic selection. C++ resolves this with a rule that, when there are multiple possible instantiations, the selected one must be one notch "better" than the alternatives.

Python gets into mixed-mode trouble with numpy arrays vs. built-in lists. In Python, "[2,4] * 2" is [2,4,2,4], while "array(2,4) * 2" is [4,8].

It's a classic problem in language design. If there are no implicit conversions, users complain about having to write "1.0" instead of "1", or worse, having to write "1.0L". If there are too many implicit conversions, you get unexpected semantics. Basically, conversions are there for numbers. Then somebody decides to generalize them. That sometimes ends badly.

There's nothing here about what Rust does about conversions, but that may be covered in one of the other many parts of this series.

The good news is that the Rust programming book is finally coming out in just 6 more days. So there's something better to read than this.

[1] https://newrustacean.com/show_notes/e024/struct.script

Re:Rust Traits Deep Dive, Part 2 by chrismorgan on Wednesday June 20, 2018 @09:00AM (#17354921)
To say it was equivalent to b.add(a) was incorrect and unwise. But to say a.add(b) or Add::add(a, b) was correct. It used to be more complex in the distant past when the operators worked by reference, but since some time before 1.0 they operate by value, and so the following are all equivalent:

• `a + b`

• `a.add(b)` (provided std::ops::Add is imported and the type of a doesn’t have an intrinsic method named add, which would take precedence)

• `Add::add(a, b)` (provided std::ops::Add is imported)

• `<A as ::std::ops::Add<B>>::add(a, b)`, where A and B are the types of a and b (provided we’re not no_std).

(Note that `a.add(b)` would work if a is of type A and &A implements Add but A doesn’t, because of autoref/autoderef in method calls, while the other three approaches would not work, and would instead want you to write &a.)

Demonstration of most of this: https://play.rust-lang.org/?gist=c70a8c62fa0f304b1a8dceddf98...

Re:Rust Traits Deep Dive, Part 2 by cesarb on Wednesday June 20, 2018 @10:30AM (#17355722)
> (Note that `a.add(b)` would work if a is of type A and &A implements Add but A doesn’t, because of autoref/autoderef in method calls, while the other three approaches would not work, and would instead want you to write &a.)

That implies to me that "`Add::add(a, b)` (provided std::ops::Add is imported)" would be the most didactic explanation.

Re:Rust Traits Deep Dive, Part 2 by killercup on Wednesday June 20, 2018 @04:14AM (#17353471)
> Who determines the output type of "a + b"? "a"? "b"? Both? Are there implicit type conversions?

The Add trait is defined in `std::ops::Add` as:

    pub trait Add<RHS = Self> {
        type Output;
        fn add(self, rhs: RHS) -> Self::Output;
    }
(You can find the documentation on <https://doc.rust-lang.org/1.26.2/std/ops/trait.Add.html>)

Both the type of the `self` parameter and the concrete type of `RHS` determines which trait impl matches. For example: `2 + 2` chooses the `impl Add<i32> for i32 { type Output = i32; ... }` implementation (literal integers are of type `i32`). Something like `2 + &2` would choose `impl<'a> Add<&'a i32> for i32`.

> There's nothing here about what Rust does about conversions, but that may be covered in one of the other many parts of this series.

With operators, there are no implicit conversions involved, AFAIK. So all Rust can do is try and infer the type you want. For literal numbers, it can do so in a limited fashion. As seen above, if you write `let x = 42;` the type is inferred as `i32`. If you write `foo(666)` with `fn foo(x: u64)`, the literal 666 is inferred to be meant as `666u64`.

> The good news is that the Rust programming book is finally coming out in just 6 more days. So there's something better to read than this.

You can already read it here: https://doc.rust-lang.org/book/second-edition/index.html :)

Re:Rust Traits Deep Dive, Part 2 by chrismorgan on Wednesday June 20, 2018 @05:03AM (#17353663)
To elucidate on this: Rust’s trait implementation coherence rules ensure structurally that there is never any ambiguity to be resolved. If I `impl<T: MyTrait> Add<T> for MyType`, I am then barred from writing `impl Add<AnotherType> for MyType` if AnotherType implements MyTrait, as that would cause a conflict, but I am permitted to if it does not, because there is then no conflict. Also you can’t implement any form of Add for MyType from your crate, because you can only implement traits from your own crate on any type, or traits from any trait on your own type, not traits from other crates for types from other crates.

(I’m still simplifying this a little, because specialisation is a thing which does allow overlapping implementations, but it must be opted into by the broad implementation by writing `default fn` instead of `fn`.)

Re:Rust Traits Deep Dive, Part 2 by kaoD on Friday June 22, 2018 @04:54AM (#17372514)
Actually you can do that:

http://play.rust-lang.org/?gist=566c7ddb5ec90836004a2549aa78...

Because `impl Add<Bar>` and `impl Add<Baz>` are two clearly different impls there is no ambiguity (RHS is known at compile time).

What specialization would solve is this kind of ambiguity:

http://play.rust-lang.org/?gist=34142671953f75f0448b03bb804f...

Because `impl<T: Debug> Add<T>` and `impl Add<Baz>` are conflicting, since Baz is `:Debug` so both impls apply for a Baz RHS.

Re:Rust Traits Deep Dive, Part 2 by chrismorgan on Wednesday June 20, 2018 @05:05AM (#17353670)
> With operators, there are no implicit conversions involved, AFAIK.

Beyond autoref and autoderef, anyway.

Re:Rust Traits Deep Dive, Part 2 by red75prime on Wednesday June 20, 2018 @04:54AM (#17353636)
> if you write `let x = 42;` the type is inferred as `i32`.

It can be influenced by the x's usage.

    let x = 42;
    let y = x/2 + 21u32;
x is u32 here.
Re:Rust Traits Deep Dive, Part 2 by simias on Wednesday June 20, 2018 @07:55AM (#17354480)
Yes the rule is that it defaults to i32 only if there's absolutely no way to infer the exact type by use like in your example. So in practice it almost never happens because as soon as you use the value in an operation or function call the compiler knows the exact type. One exception would be code like:

    fn main() {
        for i in 0..10 {
            println!("{}", i);
        }
    }
Here there's no way for the compiler to know what exact integer type is expected so it defaults to i32. There was a debate about this, I was personally in opposition but in my experience it mostly occurs on small "hello world" examples and code snippets so it doesn't really matter. I've never encountered any issues stemming from this rule so far. At least they got rid of the "int" and "uint" types which is a great thing in my opinion.
Re:Rust Traits Deep Dive, Part 2 by masklinn on Wednesday June 20, 2018 @07:59AM (#17354505)
GP is talking about the default value for "unconstrained" literals, yours is constrained by the second expression: assuming just the stdlib,

* Add<u32> is implemented for u32 and &u32

* thus x/2: (u32 | &u32)

* there's no Div<Output=&u32> so x/2: u32

* Div<Output=u32> only is only "Div<u32> for u32"

* thus x: u32

Re:Rust Traits Deep Dive, Part 2 by dorfsmay on Wednesday June 20, 2018 @11:59AM (#17356569)
Interesting... I'm a newbie at Rust, and expected y to be a float, because of the division.

But it turns out

    let x = 11;
    let y = 11/2;
==> y = 5 Given how type safety is important to Rust, I expected this to panic rather than do an automatic cast.
Re:Rust Traits Deep Dive, Part 2 by masklinn on Wednesday June 20, 2018 @12:19PM (#17356774)
There is no type safety issue, and there is no cast. Div on integral types is explicitly implementing as an integer/truncating division: https://doc.rust-lang.org/src/core/ops/arith.rs.html#436-452

There would be no type-safety issue or cast if it were implemented as a real division either, incidentally.

There is room to argue Div should not have been implemented at all on integral types (as in Haskell where integer division is a separate operation entirely), but that's a completely different issue.

Re:Rust Traits Deep Dive, Part 2 by dbaupp on Wednesday June 20, 2018 @08:30AM (#17354720)
> But which one of those?

The situation is far simpler than that sentence implies; a + b is (always) equivalent to Add::add(a, b). The same trait/impl rules apply to + as to normal trait function calls.

> Are there implicit type conversions?

No, as people quickly encounter if they do numeric/matrix computation in Rust, all type conversions are explicit.

> The good news is that the Rust programming book is finally coming out in just 6 more days. So there's something better to read than this.

Even better news: that particular Rust book has been available online for a long time, as the official documentation.

Re:Rust Traits Deep Dive, Part 2 by steveklabnik on Wednesday June 20, 2018 @09:01AM (#17354931)
Tiny, tiny addendum here:

> a + b is (always) equivalent to Add::add(a, b)

method call synatx does auto ref/deref, so it may be

Add::add(&mut a, b)

or

Add::add(&a, b)

too, etc.

Re:Rust Traits Deep Dive, Part 2 by dbaupp on Wednesday June 20, 2018 @09:34AM (#17355165)
Yep! That's why it's equivalent to Add::add(a, b), not a.add(b). (This was definitely something I had to check, though, and, as you say, a tiny detail that's almost certainly irrelevant to most code that people write.)
Re:Rust Traits Deep Dive, Part 2 by chrismorgan on Wednesday June 20, 2018 @09:40AM (#17355213)
Ah, the old days when operators worked by reference… it was much harder to express precisely what the + sugar was back then.
Re:Rust Traits Deep Dive, Part 2 by steveklabnik on Wednesday June 20, 2018 @09:37AM (#17355186)
See my response to Chris, I actually thought it was! My bad!
Re:Rust Traits Deep Dive, Part 2 by chrismorgan on Wednesday June 20, 2018 @09:31AM (#17355141)
Method call syntax does, but operators don’t.
Re:Rust Traits Deep Dive, Part 2 by steveklabnik on Wednesday June 20, 2018 @09:36AM (#17355183)
Ah ha, I didn't realize that. I thought it desugared to the method call, which then gets de-sugared to the fully qualified syntax. That does seem more complicated than skipping the intermediate step, and since we have so many implementations for the various ref or not'd version, it ends up feeling the same. Thanks!
Re:Rust Traits Deep Dive, Part 2 by majewsky on Wednesday June 20, 2018 @09:51AM (#17355293)
> If there are no implicit conversions, users complain about having to write "1.0" instead of "1", or worse, having to write "1.0L". If there are too many implicit conversions, you get unexpected semantics. Basically, conversions are there for numbers. Then somebody decides to generalize them. That sometimes ends badly.

In Go, there are special types for integer and float literals. Literals can be implicitly casted into any numeric type large enough to store them, but casts from numeric types into other numeric types must be explicit. So for example:

  funcThatTakesInt32(1) // works
  funcThatTakesUint64(1) // works too

  var x int32 = 1
  funcThatTakesUint64(x) // error: type mismatch
  funcThatTakesUint64(uint64(x)) // works: explicit cast
The only thing I'm not sure about is which numeric type gets chosen when a literal is stored in a fresh variable without declaring its type, i.e.

  x := 42
  y := 23.0
I think `x` would be `int` since the following works:

  for x := 0; x < 10; x++ {
    fmt.Println(someArray[x]) //subscript requires `int` index
  }
But then what happens when the literal in `x := <literal>` is too large to fit in `int`? And since the size of `int` (AFAIK) is machine-dependent, does that mean that some programs will compile for some target platforms, but fail for other target platforms? Argh. I see what you're getting at.
Re:Rust Traits Deep Dive, Part 2 by kbenson on Wednesday June 20, 2018 @12:36PM (#17356940)
> If there are no implicit conversions, users complain about having to write "1.0" instead of "1", or worse, having to write "1.0L". If there are too many implicit conversions, you get unexpected semantics.

There is another way out of this, which is using different operators. Modern Unicode can help greatly with this, but even before that was common, Perl was doing something similar between some of its major types (numbers and strings). Since Perl types are dynamic, and may be used as a string or a number, there would be a lot of ambiguity on comparing two variables for equivalence if the comparison depended on the type. To solve this, Perl has two sets of operators, one for dealing strings (eq,ne,gt,lt,gte,lte, '.' for concatenation), and one for dealing with numbers (==,!=,<,>,<=,>=, + for addition). What these primarily do is interpret both sides as the desired type before the comparison. This greatly reduces ambiguity, but does require more operators be internalized before language proficiency is achieved.

This is of course much harder to accomplish and becomes much more complicated as you increase the number of types. That said, it's an important concept and could help quite a bit in circumstances where things can be ambiguous (for example, your Python example).

Re:Rust Traits Deep Dive, Part 2 by lomnakkus on Wednesday June 20, 2018 @03:54PM (#17359119)
Another way would be to use return type polymorphism. That way you could have a trait similar to Haskell's "Num a" + "Integral a" with "fromIntegral :: a" and just have the parser insert a "fromIntegral" in front of every numeric literal.

I don't know much about Rust, but I'd assume from the discussion that it doesn't support this?

Of course, Haskell's Num is a horribly broken typeclass, so don't do exactly that. :)

Re:Rust Traits Deep Dive, Part 2 by masklinn on Wednesday June 20, 2018 @07:19AM (#17354289)
> Are there implicit type conversions?

Only implicit refs and derefs I think.

> It's a classic problem in language design. If there are no implicit conversions, users complain about having to write "1.0" instead of "1", or worse, having to write "1.0L".

Rust handles that by type-inferring literals, the literal itself does not impose a type.

If there are no constraints at all I think it defaults to i32. The type can also be provided explicitly as part of the literal e.g. 1f32 or 7u8.

Re:Rust Traits Deep Dive, Part 2 by chrismorgan on Wednesday June 20, 2018 @08:41AM (#17354805)
Note that integer literals and float literals are still distinct; `1` cannot be interpreted as a float, nor can `1.0` be interpreted as an integer—though `1f32` is actually legal (this surprised me; I don’t know when it became legal), while `1.0u32` isn’t.
Re:Rust Traits Deep Dive, Part 2 by smaddox on Wednesday June 20, 2018 @10:02AM (#17355397)
I'm a bit surprised this isn't covered in any of the documentation, but it might just be because the semantics are clear once you understand associated types. The `Add` trait has an associated type `Output` that you must define when implementing `Add`. The semantics are then that the type on the left decides the output, but that type can change with the type on the right. It is up to the type author to provide appropriate symmetric implementations to avoid the confusion you describe.
Re:Rust Traits Deep Dive, Part 2 by atombender on Wednesday June 20, 2018 @11:56AM (#17356542)
Thanks for the link to the script. I was about to dismiss this story because it's not at all obvious from that page that there's a text version.

This is a web site of presentations/lessons which has been generated from source code by a documentation generator, with each Rust module representing a topic. It's "cute", but really friendly to humans.

Re:Rust Traits Deep Dive, Part 2 by renox on Wednesday June 20, 2018 @06:59PM (#17360664)
Your examples show (again) that it is a mistake to conflate aritmetic operators and concatenation operators: the first are commutative the second aren't. D has ~ for concatenation which is nice but it has nothing for "concatenative multiplication" (if memory serves)
Re:Rust Traits Deep Dive, Part 2 by kjeetgill on Wednesday June 20, 2018 @06:46AM (#17354093)
Java can get into some weird cases with generics, super classes, and overriding. Joshua Bloc has some interesting ones in the fantastic (and horrific!) book, "Java Puzzlers: Traps, Pitfalls, and Corner Cases".

For what it's worth, I think python's case is fairly simple and easy to remember.

Python objects can define two different methods for operator * : __mul__(self, b) and __rmul__(self, b). When you see A * B, try A.__mul__(B) if A has a __mul__. If the call throws NotImplemented (usually due to B's type) of if A did not hava a __mul__ call B.__rmul__().

I think that's actually much simpler that relying on types for dispatch since operators tend to be more "flexible" in their operand types than normal functions.

Re:Rust Traits Deep Dive, Part 2 by mlevental on Wednesday June 20, 2018 @10:06AM (#17355439)
is the script available for only certain episodes? i can't find links and fiddling with the url doesn't work consistently. e.g.

https://newrustacean.com/show_notes/e023/struct.script

but not

https://newrustacean.com/show_notes/e000/struct.script

Re:Rust Traits Deep Dive, Part 2 by chriskrycho on Thursday June 21, 2018 @12:06PM (#17366380)
I haven't gotten around to back-porting the scripts for all episodes, but you can find them (early ones less fleshed out; I started scripting top-to-bottom relatively early, though) in the repository, here: https://github.com/chriskrycho/newrustacean.com/tree/master/...
Re:Rust Traits Deep Dive, Part 2 by mlevental on Friday June 22, 2018 @08:43AM (#17373494)
thanks