3 ms·
It does. This could have been written .map_err(ToString::to_string) as well. Works just fine with methods.
by steveklabnik 10y ago
It does. This could have been written
.map_err(ToString::to_string)
as well. Works just fine with methods.
- dan00 10y agoWhy can't it be written as: .map_err(to_string) When using the lambda the type is inferred, so why should there be a need for the ToString?
- steveklabnik 10y agoIf `to_string` was a function that was imported into the local namespace, you could. But since it's a method, you can't; you need to provide the trait name.
- steveklabnik 10y agoTo follow up on this slightly, it's not really that this is special syntax. It's that map_err takes a function as an argument, and this is how you refer to this method by name.
- dan00 10y agoIf there's a function and a method named to_string the user would've to be explicit about which one he uses by adding a namespace, like ToString. If there's only one to_string function or method, the compiler could just take this one.
- MichaelGG 10y agoIt'd need syntax to distinguish from a local function called to_string. Anything less allows ambiguity and wouldn't be equivalent (like using type::method won't do auto-borrow). So it'd need to be "\to_string", "|to_string" or something. Ample opportunity for bikeshedding. Functional style is hampered by excessive verbosity. (Non functional style is so verbose a bit of extra noise doesn't hurt _as much_.) Rust could use a lot more inference, custom operators[1], and so on. They seem to sort of agree, with auto-deref, auto-borrow, some type inference, but won't go all the way. I suppose being conservative can be defended -- can't go back without breaking code. Hopefully, in the future, the verbosity will annoy more people and there will be enough support to head in a more Haskell/ML direction. But they seem very opposed to it at the moment. 1: The rationale apparently being "someone might abuse it!" instead of "it makes good libraries even better". Parser combinators, UI toolkit code do great with custom operators. Require a method name (i.e. operator !!= as foo) if it's too great a concern. Can't save yourself from bad writers. Crippling yourself to avoid this seems like a poor tradeoff.
- MichaelGG 10y agoBut that's actually longer. It does look a bit better though, maybe.
- steveklabnik 10y agoYeah, it's not about saving characters to me, it's about clarity.
- MichaelGG 10y agoActually, this suggestion does not even work. Writing trait::method is not equivalent to writing "|x|x.method()". The latter will use method lookup rules, the former requires the programmer to decide which impl. For instance, in the above example, if the type impl'd to_string, that would be the one used, not ToString's implementation. From what I can tell anyways: https://is.gd/E8pdWc https://is.gd/E8pdWc Edit: Also, auto-borrow does not seem to work with this syntax. This is a common enough pattern that reducing the visual noise will increase clarity.
- steveklabnik 10y agoYes, this is correct. It's what I was getting at in the other thread; this is choosing a method manually.