-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Closed
Description
The example is about demonstrating the usefulness of phantom types by implementing the Add trait for a Length type.
I think it is unnecessary to do it over a generic type:
struct Length<Unit, T>(T,PhantomData<Unit>);
should just be
struct Length<Unit>(f64,PhantomData<Unit>);
(or f32
as in the example)
The implementation:
impl<Unit, T> Add<Length<Unit, T>> for Length<Unit, T> where
T: Add<T, Output=T> + Clone + Copy {
type Output = Length<Unit, T>;
fn add(self, r: Length<Unit, T>) -> Length<Unit, T> {
Length(self.0 + r.0, PhantomData)
}
}
can then be written:
impl<Unit> Add for Length<Unit> {
type Output = Length<Unit>;
fn add(self, rhs: Length<Unit>) -> Length<Unit> {
Length(self.0 + rhs.0, PhantomData)
}
}
Unless being able to implement Length over several type (f32, f64, i32, ...) is needed, there is no point to it, and I think this is not the point of this example (but I might be wrong).
Metadata
Metadata
Assignees
Labels
No labels