-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Idea: separate types for polar/Cartesian represendations #19
Comments
As far as I can tell, there are (at least) two ways that this could be implemented. One is to have separate structs for each type: pub struct Cartesian<T> {}
impl<T> Cartesian<T> {
pub fn to_polar(self) -> Polar<T> {}
}
pub struct Polar<T> {}
impl<T> Polar<T> {
pub fn to_cartesian(self) -> Cartesian<T> {}
} with methods to convert between them. Another way would be to add a second type to use std::marker::PhantomData;
// Representations
type Cartesian;
type Polar;
// R is the marker for represetation
pub struct Complex<T, R> {
a: T,
b: T,
repr: PhantomData<R>
}
impl<T> Complex<T, Cartesian> {
pub fn to_polar(self) -> Complex<T, Polar> {}
}
impl<T> Complex<T, Polar> {
pub fn to_cartesian(self) -> Complex<T, Cartesian> {}
} where The advantage of the second method would be less code repetition for shared methods, but I'm not sure about the amount of those. The negative would be obviously the more complex code which may bot be as beginner friendly as the current methods. The advantage of the first method would be that it is clear that these are distinct types, but methods in common must be implemented separately. Either method would be a breaking change if used as is, but they could both be used behind a wrapper type that is named |
The branch is currently on 0.2.0-git (pre-release), so breaking changes are possible. I'm inclined to leave We could add |
I also prefer |
@clarcharr Did you ever experiment with |
@cuviper I actually completely forgot, although I have a feeling it would make a lot of computations simpler. |
This might allow users to control which representation is used for the sake of increasing efficiency of calculations.
For example, if a program needs to multiply a large amount of numbers together, they can convert to polar and then back to Cartesian after all of the operations are done.
The text was updated successfully, but these errors were encountered: