I'm attempting to make a Result
with a lifetime parameter, as an additional restrain on the T
(::Ok(T)
) and E
(::Err(E)
) generic parameters.
// enums A<'a> and B<'a> elided
pub enum Result<'a, T=A<'a>, E=B<'a>> {
Ok(T),
Err(E),
}
unused parameter, suggestion: add PhantomData
So it didn't work. I read that I can hack around the unused parameter error with PhantomData
, but that seems dirty to me.
Neither did this work:
pub enum Result<T=A<'a>, E=B<'a>> {
Ok(T),
Err(E),
}
undefined parameter, suggestion: add parameter before T
(twice)
This suggestion throws me for a loop!
How do I put a generic type
with a lifetime parameter, in my enum
generic parameter default value?
The error is correct, this type would make no sense:
Since the lifetime is only used in default parameters, what would the lifetime be in cases like this:
?
The lifetime isn't used, this is where the error message comes from.
The question doesn't provide much context, but it does seem like you intend to only use
A<'_>
andB<'_>
in your type. If that is the case, then you must usewhich is valid. However that would raise the question of why you're not using the built-in
Result
type, maybe combined with a type alias to avoid repetition:type Result<'a> = std::result::Result<A<'a>, B<'a>>;
. This is pretty common to do, even the standard library does it a few times:std::io::Result
:type Result<T> = Result<T, Error>;
std::fmt::Result
:type Result = Result<(), Error>;