2
나는 녹 이에 대해 implementing tuple flattening입니다. 그것은기본 방법에서 특수화 된 관련 유형을 반환하려면 어떻게해야합니까?
내가 전문화를 사용하여 시도Cons[
Cons[A, B, Nil],
Cons[
C, Cons[D, E, Nil], Nil
],
F,
Nil
]
에
((A,B), (C, (D, E)), F)
변환이 필요하지만 컴파일러는 그것을 좋아하지 않습니다
/// For non-tuple types.
impl<T> IntoCons for Val<T> {
default type Out = Cons<T, Nil>;
default fn into_cons(self) -> Cons<T, Nil> {
Cons {
head: self,
tail: Nil,
}
}
}
내가 어떻게 할 수 있습니까? unsafe
을 사용하지 않는 대안은 괜찮습니다.
전체 예제 :
#![feature(specialization)]
use std::fmt::{Debug, Display};
pub trait Tr {
type It;
fn it(self) -> Self::It;
}
impl<T> Tr for T
where
T: Debug,
{
default type It = u8;
default fn it(self) -> Self::It {
0
}
}
impl<T> Tr for T
where
T: Debug + Display,
{
type It = u16;
fn it(self) -> Self::It {
0
}
}
fn main() {}
컴파일러 출력 :
error[E0308]: mismatched types
--> src/main.rs:17:9
|
16 | default fn it(self) -> Self::It {
| -------- expected `<T as Tr>::It` because of return type
17 | 0
| ^expected associated type, found integral variable
|
= note: expected type `<T as Tr>::It`
found type `{integer}`
* 누군가가 그것은 존재와이를 구현한다면'String'은 * 어떻게됩니까 - 이것은에서 구현되지 않습니다 * 형질 *, 담요 구현. 'default'와 specialized impl을 제거하면 (https://play.integer32.com/?gist=bbeb840a5ab82e605c2ad8e35938bc76&version=stable) 코드가 작동합니다. 특별한 구현을 일반 구현과 다른 점은 무엇입니까? – Shepmaster
이 경우 콘크리트 유형입니다. 컴파일하려면 '기본 유형 It = u8;'만 있으면됩니다. Default 특성 바인딩을 사용하므로 해당 구현이 항상 작동합니다. 여기를 참조하십시오 : https://play.rust-lang.org/?gist=0f5da74bbfbbf4f818cefa134d27f4fc&version=nightly – Neikos
Default : default()에서 cretead가 될 수있는 간단한 것을 반환하고 싶지 않지만이 대답을 내 예가 잘못되어이 문제를 해결하기 위해 필자는 문제를 해결할 것입니다. 나는 그것이 영원히 불안정한 것처럼 보이기 때문에 난관을 사용하고 싶지 않았지만 효과가 있었고, 전문화 기능이 그 경우를 개선하기를 기대한다. – kdy