Dhghomon / rust-fsharp

Rust - F# - Rust reference
MIT License
239 stars 14 forks source link

Fix #24 #26

Closed Happypig375 closed 3 years ago

Dhghomon commented 3 years ago

Looking good so far but I'm not going to bring in the impl IntoIterator into the example as mentioned before - this is a book to ease people from one language into the other (not scare them away). The example should be about as complex as this one:

https://doc.rust-lang.org/rust-by-example/trait/iter.html

(Note it only implements Iterator, then uses it in a for loop - that's enough for a newbie to the language to take in at first IMO. Could even change the example struct to something simpler if it would make for a better example - doesn't necessarily have to be a Metropolis)

As a mostly newie to F# too I can comment on the other example. Looks like it could be shortened a bit too.

I'm curious about this part:

While implementing these interfaces, you must also implement the outdated non-generic versions that are essentially replaced by the generic versions in 2005 with .NET Framework 2.0.

Is this permanent boilerplate then? This is the part that really jumps out at me.

() = new IntoIter(this) etc. I assume we can take these parts out if the Rust example is as simple as it should be for a newcomer

member this.MoveNext() = // This is very similar to Rust's next except that this returns whether it's a Some, while the value is assigned a mutable variable read by the Current property This is cool to know

Everything else looks readable enough I think and nice comments on the right.

Thanks!

Happypig375 commented 3 years ago

F#'s for loops and library functions do not support iterators directly, instead they operate in iterables that can produce iterators on-demand. If IntoIterator is idiomatic in Rust, why not parallel it with F#? As you said, most Rust introductions assume the reader to be dumb. This is not the case here.

Happypig375 commented 3 years ago

I'll add a comment about the :> operator.

Is this permanent boilerplate then? This is the part that really jumps out at me.

Yes. But in practice it is rare that you would implement these interfaces directly, instead you would access the actual collections and iterate them. Moreover, the boilerplate isn't that severe - it's just 4 additional lines of boilerplate (interface ... with / member GetEnumerator / interface ... with / member Current) that are extra compared to a clean design.

Dhghomon commented 3 years ago

Think of it as more of a "here's how to start getting things done in the other language based on what you know already" book. (Roughly the equivalent of the F# books for C# developers, which also don't go straight off the deep end)

Take the word iterable for example - it doesn't even appear in the Rust book. https://www.google.com/search?q=site%3Ahttps%3A%2F%2Fdoc.rust-lang.org%2Fbook%2F+%22iterable%22&sxsrf=ALeKk029wZa1vn6ZvqaohM63d2yXWLhM7A%3A1622822542353&ei=jk66YKb0FImD0wSU8aHIBg&oq=site%3Ahttps%3A%2F%2Fdoc.rust-lang.org%2Fbook%2F+%22iterable%22&gs_lcp=Cgdnd3Mtd2l6EANQxfcEWI-BBWD_gQVoAnAAeACAAXCIAZEJkgEEMC4xMZgBAKABAaoBB2d3cy13aXrAAQE&sclient=gws-wiz&ved=0ahUKEwim862Jrf7wAhWJwZQKHZR4CGkQ4dUDCA4&uact=5

Of course, that all depends on the writing. If it starts with a simple Iterator example and then eases into a more complex one, that's fine. Any level of complexity is fine if it's one step at a time.

Dhghomon commented 3 years ago

Okay, having said my piece I'll merge all of these! This is all very helpful. But please remove the 'iterable' wording and add the simpler impl Iterator example before. (Or I'll do it tomorrow when I wake up - it's nighttime over here)

Dhghomon commented 3 years ago

"Add the simpler impl Iterator" as in like a warmup for the larger one.