« first day (1103 days earlier)      last day (2368 days later) » 

9:38 AM
@Shepmaster Wherein people who write parsers in C++ accept their fate, those who try to in Rust seek to be consoled.
 
 
3 hours later…
1:05 PM
Released bidir-map 1.0.0 with Index impl. How do you like the usage of By{First,Second} therewith?
 
1:22 PM
I find it has the same problem has std::pair in C++: first and second are semantically "null". I'm not sure if it's a big problem here, since the map itself provides context; it's the kind of things that experience will tell.
 
@MatthieuM. Not sure what you mean by "semantically null"?
 
1:39 PM
I mean that it has no associated semantics. It's often a reproach made to std::pair, for example it is idiomatic that a single element insert in an associative container will return std::pair<iterator, bool> where the iterator points at the element in the container and bool indicates whether it's freshly inserted or not
However, when you access pair.second, it certainly does not read inserted.
 
I have this code which I'm not able to compile, and I don't understand the error message
```
let tuple: (&str, &str) = {
if is_celsius {
return ("Fahrenheit", "Celsius");
}

return ("Celsius", "Fahrenheit");
};
```
 
Another example is storing a pair as the value in a map; access through iterator is it->second.first and it->second.second... at this point it hardly conveys what's accessed! People have argued that having it->value.bar would be more meaningful to the reader.
@simeg: You do realize that return returns from the function, not the block?
This should really be let tuple = if is_celsius { ("F", "C") } else { ("C", "F") } I think.
(+ semicolon after closing brace)
 
22 |             return ("Fahrenheit", "Celsius");
   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found tuple
@MatthieuM. even though I try without return and ; I get the same message
sorry I'm on a train so connection is a bit off
@MatthieuM. yup your code works. I can't see how it's any different from mine (without return and ;). But thanks!
 
 
1 hour later…
2:55 PM
@MatthieuM. ah
 
3:34 PM
@simeg return and ; are vitally important. return exits the function, not the expression.
@simeg And if you had let tuple = if is_celsius { ("F", "C"); } else { ("C", "F"); };, the semicolons after the tuples converts the tuple expression (which has a useful value) into a statement (which has the unit value ()).
 
 
4 hours later…
7:47 PM
hi everyone :)
I wonder if there's a name for the Rust pattern where a function accepting different types is emulated using a generic function with a trait bound
 
8:02 PM
@user4815162342 Not sure, but is it one of the handshake patterns?
 
maybe, but it sounds way too general
this kind of thing is when you want a method that accepts an argument, and you want it to accept either a string, a number, or some Enum
(it sounds silly when said like that, but when you're writing ergonomic builder-style APIs, you really want that kind of thing)
I'm positive I didn't invent it, I saw it used in stdlib and elsewhere...
and I think you can't use the standard Into trait because you want to define the implementation of the trait for types outside your crate, which you can't do if neither the type nor the trait is defined by you
so it has to be IntoMyThing
a related pattern (but less flexible) are the filesystem-related functions in the stdlib accepting T where T: AsRef<OsStr>
 
Maybe there isn't a name for it..? :/ We could use something like the Rust cookbook, but for patterns. I've read about a good bunch of patterns, but I've never seen an aggregation of them.
 
8:30 PM
@user4815162342 /me waves
I'd agree that I haven't heard a name for it, although I feel like the nuance of "trait for types outside your crate" would be a subpattern of a kind
Cause normally I'd work around that by having my own type that everything is going to
 
@Shepmaster I believe that's exactly the case here
the function logically accepts a type Foo
but it also knows how to convert three other types into Foo, some of which are implemented in the stdlib
and it's really convenient for it to accept those other types as well
then the author creates an IntoFoo trait with a single into_foo(self) -> Foo method
the function accepts a foo: T, where T: IntoFoo and the first thing it does is let foo = foo.into_foo()
 
@user4815162342 But in that case you own Foo, so can't you implement From?
 
the thing is, I'm not sure I want Foo to be convertible from all these types everywhere
but only in the context of this function, or this set of functions
 
This clearly needs a name. :)
 
it's just like overloading in C++ :)
except it works completely statically
this stdin() example works nicely to explain the concept
you want to be able to do
let out = Exec::cmd("sort")
.stdin("b\nc\na\n")
but also:
let out = Exec::cmd("sort")
.stdin(Redirection::Pipe)
 
8:42 PM
@user4815162342 Function overloading in C++ is also static...
 
What would be the dislike of creating a specific type for these use cases ?
 
yes, sorry, I meant Python
 
Ah, right.
 
in C++ you have a different problem that you have to have separate functions
 
Whatever can be put against Python, I'm in.
 
8:42 PM
here this is only one function, and it's perfectly fine to say foo.into_foo()
ha ha, I actually like both Python and Rust
 
@user4815162342 What?
 
also, you want to be able to give it a Vec<u8> in case your data is not UTF-8
or a File value, as a shorthand for Redirection::File(file_value)
this kind of thing makes the builder-style API very ergonomic
but I think it'd be wrong to define a From with such sweeping conversions
but maybe that'd be fine... after all, this type is pretty much invented for this function
maybe I simply reimplemented From :/
:-D
 
That is indeed nice, and an appropriate use of the Rust type system.
@user4815162342 However... I don't think C++ has a problem here, unless I overlooked something.
 
It really feels like you would want a ProcessBuilderStdin with all the implementations of From.
 
@She
@Shepmaster I sense a new release of the subprocess crate :)
@E_net4 I guess I just dislike the C++/Java approach to overloading, which is to define a dozen different functions with all combinations of arguments. I'm glad Rust didn't follow that path.
I do wish we had optional arguments in Rust, though... that'd make many APIs much more ergonomic
 
8:57 PM
@user4815162342 Oh, especially since IntoInputRedirection is a private trait? So I can't implement it myself or click to see what implements it. Sad times.
 
yeah, it's an implementation detail, really
 
well, if I have my own interesting type, I can't participate in the abstraction
 
@user4815162342 Good point. In C++, one would either make multiple implementations or use templates to exploit a common API.
And without Concepts, this can be a bit chaotic.
 
@Shepmaster in this case that's a feature - the function is defined to do these exact things
it's simply not designed to be extensible (in that way)
and if you have a type that can be coerced into something the function understands, it's not hard to add a "to_foo" method and call .stdin(bla.to_foo()) instead of .stdin(bla)
 
@user4815162342 I often choose to employ the builder pattern when I expect a carpload of optional arguments.
 
9:02 PM
@E_net4 same here
in fact, subprocess has two APIs: one that works with a "configuration" struct that contains a bunch of Options
and the builder API that basically builds up this structure and calls the lower-level API
the Configuration stuff mirrors the kind of keyword arguments you'd be sending to the original Python API
and the builder is supposed to be as Rustic as possible, but retain the power of the original
I think it's still useful to have the low-level variant public because it might be easier to use if you want to fill the struct yourself with a weird combination of options that might be cumbersome to program using the builder
 
@user4815162342 in case you wanted to make your trait public but prevent people from implementing it - github.com/rayon-rs/rayon/blob/…
 

« first day (1103 days earlier)      last day (2368 days later) »