4 ms·
I love when authors add how to read aloud something to help newcomers, as in fn print<T: ToString>(v: T) { > We say that “print is generic over type T, wher
by riquito 6y ago
I love when authors add how to read aloud something to help newcomers, as in
fn print<T: ToString>(v: T) {
> We say that “print is generic over type T, where T implements Stringify”
- aspaceman 6y agoI also greatly appreciate this. Thanks for pointing it out for me!
- seaish 6y agoDoes anyone actually call it "Stringify"? I would call it "to string".
- smabie 6y agoProbably not. If they wanted people to call it Stringify then just name it stringify. Though personally, I like how instead of toString, Haskell calls it 'show'. Clear, to the point, and one word.
- carlmr 6y agoHow is "show" clear for string conversion? You could just as well expect a GUI popup just going from that word.
- smabie 6y agoHow is toString, you just might expect your object to turn into a ball of yarn.
- carlmr 6y agoString is a very common but specific word in programming for a string of characters. Show is a very common but generic word in programming, for showing plots, showing message boxes, showing anything really. Not knowing Haskell I would not have expected it to convert an object to a string.
- couchand 6y agoI suppose Rust's direct analog to Show is Display. There's a blanket implementation of ToString for any type implementing Display, so ToString can be thought of as a super-type. "Display" is nice because it is usually uttered in the same breath as Debug.
- simias 6y agoRust has Display: https://doc.rust-lang.org/std/fmt/trait.Display.html https://doc.rust-lang.org/std/fmt/trait.Display.html On the doc of ToString you can find the following note: >This trait is automatically implemented for any type which implements the Display trait. As such, ToString shouldn't be implemented directly: Display should be implemented instead, and you get the ToString implementation for free. I think ToString exists mostly in order to have a more convenient API to get a String out of a display-able type, otherwise if you used Display directly you'd have to use a variant of that boilerplate every time: https://doc.rust-lang.org/src/alloc/string.rs.html#2185-2192 https://doc.rust-lang.org/src/alloc/string.rs.html#2185-2192
- chrismorgan 6y agoThat’s not an instruction on how to read the code aloud, but a way of describing a certain fact about the function. If you were actually reading it aloud, I would strongly avoid that use of the word “where”, because it’ll be confused with a where clause: fn print<T>(v: T) where T: ToString The two are semantically identical, but they put the constraint in different places, so using the word “where” will confuse things, especially when it leads to you repeating the “T” in a way that where clauses do but the first form doesn’t. For myself, I would probably read the original line of code as “function print, generic over type tee implementing to string, taking parameter vee of type tee”. The where clause version I’d read as “function print, generic over type tee, taking parameter vee of type tee, where tee implements to string”.