This would be [pre-rfc], but at present I have no idea how to correctly write an RFC, so instead I’ll just start a discussion
At present in rust, it is not possible to specify the type expected as the result of a pattern binding. This means we can’t take advantage of things like dynamic dispatch in match branches (I see dynamic dispatch of pattern bindings as the main feature of this).
The actual code that caused me to want to start a discussion of this, is as follows
match self {
&Logogram::UniMonoGram(ref a) | &Logogram::DiLogogram(ref a) => f.fmt(a),
&Logogram::DuoMonoGram(ref a, ref b) => write!(f, "({}{})", a, b),
}
both a
under UniMonoGram
which has type MonoLogogram
and a
under DiLogogram
which has type DiLogogram
implement std::fmt::Display
, but there is no way for me to instruct Rust to do dynamic dispatch here at present. In fact, it is not possible to specify the type of the two a’s at all (unless I am mistaken)
I’d propose allowing the usage of a where clause on pattern bindings, allowing you do something like this
match self {
&Logogram::UniMonoGram(ref a) where a: &Display | &Logogram::DiLogogram(ref a) where a: &Display => f.fmt(a),
&Logogram::DuoMonoGram(ref a, ref b) => write!(f, "({}{})", a, b),
}
another more minimal example of this is at present you cannot specify the types of the integers in a sequence like
let (x, y) = (12, 42);
obviously in this case this is not a problem, because you can just specify the type on the literals, but I digress
let (x, y) where x: i32, y: i32 = (12, 42);
As a note based on what myself and @durka discussed on IRC, this may seem to have some conflict with the fact you can do let ref x: i32 = 12;
but not let ref x: Display = 12;
. I’m no expert on how ref
is implemented, but I’d suggest that using where
clause on pattern bindings should run after all bindings (including ref
) are complete, so the aforementioned would look like let ref x where x: &Display = 12