You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm hitting a bug in my math library due to associated types.
The compiler does not recognize that the associated type on the result of Add is the same type as T (well, it partly does, as the error message says that Item == T, and as such, the expression c1+c2 does not typecheck.
Here is the error
error: type mismatch resolving `<core::iter::Map<(T, T), <T as core::ops::Add>::Output, core::iter::Zip<generic_vector::Components<'_, T>, generic_vector::Components<'_, T>>, closure[vector_test.rs:17:54: 17:69]> as core::iter::Iterator>::Item == T`: expected associated type, found type parameter
vector_test.rs:15:13: 15:47 note: required by `core::iter::FromIterator::from_iter`
vector_test.rs:15 std::iter::FromIterator::from_iter(self.components()
#![feature(associated_types)]use std::ops::Add;pubtraitVector<T:Copy + Add>: std::iter::FromIterator<T> + Sized{fncomponents<'a>(&'a self) -> Components<'a,T>;fnadd(self,rhs:Self) -> Self{
std::iter::FromIterator::from_iter(self.components().zip(rhs.components()).map( |(c1,c2)| c1+c2))}}/// An efficent iterator for enumerating components.////// Iterates over a collection of components in the order they exist in memory.////// This iterator is designed to be as fast and efficent as possible./// For this iterator to work correctly, `ptr` must point to a linear, continuous/// array in memory. If this requirement is not upheld, the result is undefined behaviour.pubstructComponents<'a,T>{ptr:*constT,end:*constT,}impl<'a,T>Components<'a,T>{/// Creates a new iterator from a pointer to the first component and the number of components.pubfnnew(begin:*constT,len:uint) -> Components<'a,T>{let end:*constT = ((begin asuint) + (std::mem::size_of::<T>()*len))as*constT;Components::from_ptrs(begin, end)}pubfnfrom_ptrs(begin:*constT,end:*constT) -> Components<'a,T>{Components{ptr: begin,end: end,}}}impl<'a,T:Copy>IteratorforComponents<'a,T>{typeItem = T;fnnext(&mutself) -> Option<T>{// check if we have a next componentifself.ptr != self.end{let component = unsafe{*self.ptr};// increment the pointer to the next component.self.ptr = ((self.ptrasuint) + (std::mem::size_of::<T>()))as*constT;Some(component)}else{// we reached the last componentNone}}}fnmain(){}
The text was updated successfully, but these errors were encountered:
This seems to work fine when the trait definition is changed to pub trait Vector<T: Copy + Add<Output = T>> : std::iter::FromIterator<T> + Sized (i.e. explicitly specify the result type) so I guess this may be a dupe of #19476.
I think this is just... working as designed, actually. Even if defaults were implemented, they would only affect the impls of a trait, and usages would still need to explicitly bind the result. From what the compiler knows here, the result type of the adds is just T::Output and not T.
I'm hitting a bug in my math library due to associated types.
The compiler does not recognize that the associated type on the result of
Add
is the same type asT
(well, it partly does, as the error message says thatItem == T
, and as such, the expressionc1+c2
does not typecheck.Here is the error
Code snippet (Playpen)
The text was updated successfully, but these errors were encountered: