News for hackers. Stuff that matters.
Re:Linux kernel in-tree Rust support by hellofunk on Saturday July 11, 2020 @06:44AM (#23801076)
Except that one of Linus' most vocal offenses on C++ was due to operator overloading and how basic, seemingly native things like + can actually do a lot of hidden stuff unknown to the programmer. He must have softened on this since Rust offers the same facilities for operator overloading.

https://doc.rust-lang.org/stable/rust-by-example/trait/ops.h...

Re:Linux kernel in-tree Rust support by pornel on Saturday July 11, 2020 @09:59AM (#23802258)
Operator overloading in Rust is less flexible than in C++, so there are fewer possibilities to mess it up.

Overloading is done indirectly via specific named traits which enforce method signatures, invariants, immutability rules. Assignments and moves can't be overloaded. Borrow checker limits what indexing and dereferencing overloads can do (hardly anything beyond pointer arithmetic on immutable data). Comparison operators can't be overloaded separately and can't return arbitrary values (like C++ spaceship operator). Generic code has to explicitly list what overloads it accepts (like C++ concepts), so even if you create a bizarre overload, it won't be used in generic code.

Re:Linux kernel in-tree Rust support by cletus on Saturday July 11, 2020 @06:47AM (#23801094)
Linus’s point was also that C++ makes extensive use of function/method name overloading (is polymorphism) and this can make code hard to read. Not only is this not possible in C but the feature tends to be overused in C++.
Re:Linux kernel in-tree Rust support by zozbot234 on Saturday July 11, 2020 @06:55AM (#23801127)
Rust generally uses function/method polymorphism based on traits (aka type classes), that's far less prone to overuse and abuse than overloading in C++.
Re:Linux kernel in-tree Rust support by hellofunk on Saturday July 11, 2020 @01:55PM (#23804144)
Is this form of polymorphism done at runtime and thus incurs a similar overhead to the C++ virtual?
Re:Linux kernel in-tree Rust support by steveklabnik on Saturday July 11, 2020 @02:05PM (#23804227)
There are two ways of doing it. The way most people use most of the time does not, it's statically dispatched. The other way is dynamically dispatched, though there are some differences from the way that C++ virtual works. They should be roughly the same in that case, though.
Re:Linux kernel in-tree Rust support by estebank on Saturday July 11, 2020 @02:09PM (#23804255)
It depends, you have access to both capabilities. By default if you're only dealing with an impld trait then it will use static dispatch. If you are passing around that same object as a trait objects, it will use dynamic dispatch and you can also impl Traits on other traits, which are always used as trait objects (vtable) but that have some restrictions on what they can look like. If you rely on the Deref trait to make it look like you have inheritance (but in reality it is composition) it will be the same as the later using dynamic dispatch.
Re:Linux kernel in-tree Rust support by fluffything on Monday July 13, 2020 @03:46AM (#23818178)
You can pick whether you want to do this at run-time or compile-time. The default is compile-time.
Re:Linux kernel in-tree Rust support by wheels on Saturday July 11, 2020 @08:27AM (#23801670)
> Not only is this not possible in C ...

There are lots of object systems in C that have polymorphism via function pointers.

Re:Linux kernel in-tree Rust support by jcelerier on Saturday July 11, 2020 @08:58AM (#23801884)
The linux kernel is full of those
Re:Linux kernel in-tree Rust support by CydeWeys on Saturday July 11, 2020 @09:12AM (#23801961)
I agree with this critique, but a good IDE helps, as does lint rules preventing egregious misuses (or even outlawing operator overloading entirely). As one example, many Java/C# linters prevent the use of non-curly-braced single line control statements entirely. The language allows them, but many codebases do not.