News for hackers. Stuff that matters.
Re:Rust Koans (2015) by chrismorgan on Monday May 20, 2019 @07:25PM (#19966290)
The second is about the separation of data and behaviour—Rust has structs, which are purely data, and implementations, inherent and of traits, which provide the behaviour. This is more flexible than classes which unite the two and tend to require that everything be in one place, and a Good Thing.
Re:Rust Koans (2015) by ajross on Monday May 20, 2019 @09:39PM (#19966958)
> This is more flexible than classes

Meh. Seems pretty 1:1 to me. Skipping the complexity with runtime polymorphism[1], there's no meaningful semantic difference between a Java interface or mixin-style C++ superclass or [insert abstraction from your favorite language here].

Frankly this is one of the bits of Rust that infuriates me, because while it's not a big deal nor hard to understand, it's senselessly different from the way the rest of the world does things. It's another obstacle to new programmers, in a language that is filled with booby traps for the newbie.

[1] Let me state upfront the degree to which I am completely uninterested in debating the merits of multiple virtual inheritance vs. trait objects. They both suck. I guess if I had to pick C++ sucks a tiny bit less because you can implement something like a trait object in straightforward code, where Rust can't do vtables without boilerplating every method.

Re:Rust Koans (2015) by FridgeSeal on Monday May 20, 2019 @11:31PM (#19967390)
> it's senselessly different from the way the rest of the world does things. Because every way of doing "x" has already been tried and the way we do things _right_ _now_ is hands down the absolute best way right? Less snarkily, I think experimentation is good - we come up with new approaches and ways of doing things (which could solve any number of previously difficult problems) and helps prevent "monocultural" approaches to things.

> It's another obstacle to new programmers... Why is it an obstacle rather than just something they learn? You could just as easily argue that the OO approach of wrapping up structure and functionality in a single object is equally "just another obstacle for new programmers".

> in a language that is filled with booby traps for the newbie.

That's a bit of an unfair statement, Rust has a learning curve, but it is certainly not filled with booby traps: it goes to great pains to make things transparent and be upfront about things. C++, JS or PHP are languages that I'd call filled with booby traps for beginners...

Re:Rust Koans (2015) by ajross on Tuesday May 21, 2019 @12:44AM (#19967607)
> Because every way of doing "x" has already been tried and the way we do things _right_ _now_ is hands down the absolute best way right?

Absent evidence to the contrary, yeah. Generations of hackers have been expressing designs perfectly well with traditional class syntaxes. This is a long solved problem, and a skill you can rely on when moving between C++ or Java or C# or python or Ruby or JS (though Javascript tried to get fancy in this space too and had to bolt on traditional syntax later). But to get stuff working in rust you have to learn a different metaphor. That's bad a priori unless there's a clear advantage. And be real: there isn't, it's just syntax churn.

To wit, if it ain't broke don't fix it.

Re:Rust Koans (2015) by lmm on Tuesday May 21, 2019 @06:43AM (#19968955)
> Absent evidence to the contrary, yeah. Generations of hackers have been expressing designs perfectly well with traditional class syntaxes.

On the contrary, what Rust does is a direct counter to the most notorious pitfalls of "extends" inheritance. Generations of OO experts and advocates have gone on at great length about "has a" versus "is a" relationships, about the importance of favouring composition over inheritance, about "SOLID". But these things are only communicated by oral tradition, so they remain as booby traps for every newcomer learning to design a system. It's past time that languages did more to help those newcomers (and to be fair Rust isn't the first here: Go, Kotlin, and even Java (with its separate keyword for interfaces) all made significant progress in this direction).

Re:Rust Koans (2015) by kd5bjo on Tuesday May 21, 2019 @03:52AM (#19968295)
> Generations of hackers have been expressing designs perfectly well with traditional class syntaxes.

“Traditional class syntaxes” have existed for only half the time since people started thinking about object-oriented programming in the 1950s— C++ was only invented in 1983, and didn’t get popular until the mid-90s. That puts it in widespread use for only one generation, and about due to be supplanted by the next major paradigm (maybe async/promises/futures).

It won’t go away, of course: structured, functional, and procedural programming are all standard tools used by most programmers today alongside object orientation. We just have enough experience with them to know what problems each is best and worst suited for, and this is what you’re seeing in Rust; it treats OOP as one useful tool in the toolbox instead of a panacea that makes everything better.

Re:Rust Koans (2015) by worik on Tuesday May 21, 2019 @01:29AM (#19967754)
> And be real: there isn't, it's just syntax churn

No. I have programmed in C, C++ and Rust.

Rust is much different, and it is not "just syntax ..."

Re:Rust Koans (2015) by dbaupp on Tuesday May 21, 2019 @02:07AM (#19967902)
Rust traits are similar to features in many different languages: Swift protocols, Haskell type classes, Scala traits (when used as implicit evidence), even Go interfaces (in an approximate order from most to least similar). It's not the OOP way of doing things, but it's not senseless nor is it uniquely different.
Re:Rust Koans (2015) by DougBTX on Tuesday May 21, 2019 @05:29AM (#19968682)
> there's no meaningful semantic difference

Traits can be defined for existing structs without changes to the struct definition, so there are at least practical differences.

Examples here: https://blog.theenginerd.com/blog/2015/06/27/traits-on-gener...

Re:Rust Koans (2015) by LukaD on Tuesday May 21, 2019 @05:54AM (#19968780)
Another practical difference is that you can implement multiple traits for a given struct which require a method with the same name and same/different signature. The compiler always knows which trait implementation the code is calling so the names implementations don't clash.
Re:Rust Koans (2015) by ajross on Tuesday May 21, 2019 @11:37AM (#19971133)
Not to pick on you two: but a feature that implements the same concept modulo changes to the source code like friend declarations (first example) or naming (second) is the very definition of a merely syntactic difference. Rust represents the same stuff, it just does it in funny ways. That was my point.
Re:Rust Koans (2015) by DougBTX on Thursday May 23, 2019 @02:15AM (#19988966)
Yes, I avoided the “semantic” question as it seems very slippery, it could easily devolve to “Rust is turing complete so it is just the same as any other language”.

Out of interest, do you have any examples that you would consider semantically different, while still being appropriate for day-to-day programming?

Re:Rust Koans (2015) by lmm on Tuesday May 21, 2019 @06:36AM (#19968933)
Like the borrow checker story, Rust's separation of composition, delegation, and interface implementation is not to protect them from you doing them, it's to protect you from what will happen if you do them when you didn't mean to. You talk approvingly of Java interfaces but those were controversial at the time for exactly the same reason ("why do you need a separate keyword? Just write a class full of pure-virtual functions, it's the same thing").
Re:Rust Koans (2015) by babyloneleven on Tuesday May 21, 2019 @09:58AM (#19970185)
Hey it's not senseless. Rust doesn't have subtyping (except for lifetimes), and specially not subtyping between a parent class and its child. This is on purpose. Subtyping is required for the usual class based OOP but greatly complicates type inference.

However, we don't have yet means to emulate downcasting for trait objects, except by using the Any trait (which is a footgun). Until then, class based OOP is more expressive than whatever Rust is doing now.

Re:Rust Koans (2015) by taneq on Monday May 20, 2019 @09:53PM (#19967017)
OK, so I wasn't too far off in my (not-yet-Rust-programmer) interpretation of "duck typing is good (or at least more flexible)"?
Re:Rust Koans (2015) by mintplant on Tuesday May 21, 2019 @03:55AM (#19968319)
Rust's approach is very much not "duck typing".
Re:Rust Koans (2015) by taneq on Tuesday May 21, 2019 @05:32AM (#19968691)
The koan doesn't disambiguate, though, does it? If you didn't know it was about Rust, what clues are there that it's talking about static typing?
Re:Rust Koans (2015) by morgancmartin on Tuesday May 21, 2019 @11:17AM (#19970898)
I think you're right. I took the koan to mean that when designing any given widget the focus of the implementation should make as few contextual assumptions about the use of the widget as is it feasible to do.

There is an inverse relationship between the number of contextual assumptions made about a widget and the number of contexts the widget can operate in. Something with few contextual assumptions is typically referred to as "flexible."

Duck typing as a practice entails a focus on the capabilities of a given widget rather than the "role" (read: type) of that widget. Focus on capabilities = fewer contextual assumptions. Focus on role/type = more contextual assumptions. Thus duck typing can be thought of as one possible embodiment of the koan's intended perspective.

Of course, some detractors of static typing might take this to mean that static typing implies a focus on types which as I've just shown would imply more contextual assumptions and less flexibility. But as rust's traits show, one can keep the benefits of static typing without sacrificing flexibility so long as the focus of the overall design is still capability-centric rather than role/type-centric. But that's a different conversation :)