-
Notifications
You must be signed in to change notification settings - Fork 20
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
What about traits? #5
Comments
Thank you for sparking this discussion, @nical ! I feel that traits are on the entirely opposite side of the problem. Traits describe behavior, where the original idea behind Suppose you use a library that exposes something satisfying The solution I'd prefer is to have |
The idea would be that euclid or naglebra would implement ways to make conversion to mint traits easy (like |
Something inspired from this: https://deterministic.space/elegant-apis-in-rust.html#custom-traits-for-input-parameters |
For example if you look at the lyon_bezier crate, all of the members are publicly exposed so it can't expose an API with standard types unless all members are themselves of that standard type and then I go from having a library that is very nice to use with euclid to one that is a bit awkward to use with every library (not very appealing from my point of view as a user and maintainer of the crate) and more annoying to maintain. However, the bezier curve segments could maybe be generic over a |
If you expose |
Kinda, but having mint declare data structures and logic (the traits and implementation of the traits), is equivalent to creating a new full featured math library. At this point mint isn't lighter wait than euclid or the others. Plus, for lyon_bezier to be nice to use with the rest of the lyon crates which heavily use euclid, it needs to store euclid objects (be it explicitly or generically by storing a trait that euclid points implement). If a bezier segment stores its points generically using traits (not types), I can seemlessly integrate with the rest of my code, whereas with mint types I have to jungle between two ways to represent the same thing. |
I was just about to put it into README ;)
It would be as nice to use if the rest of lyon crates was using |
One trouble with traits is that the set of potentially useful operations is much larger, and has a much larger set of design choices to be made, than the set of potentially useful interface types. Mint's current direction allows it to be lightweight and minimal, and imposes no significant design constraints on other libraries. lyon_bezier, for example, would play adequately well with mint by merit of suitable |
The traits don't need to expose the entire set of potentially useful operations. In fact it could be as small as the set of things you can do out of a mint type, so If as a user of the trait you want to add missing operations, you can simply do it, even in a separate crate. For example, injecting a trait SquareLength<T> {
fn square_length(&self) -> T;
}
impl<V, T> SquareLength<T> for V
where
V: Vector2<T>,
T: Add<Output=T> + Mul<Output=T>
{
fn square_length(&self) -> T {
self.x() * self.x() + self.y() + self.y()
}
} With mint types, lyon_bezier would have to either copy out every member into an euclid type before doing anything and then back after operations are performed (the methods are on average 5-6 lines long so it is already quite invasive), or have from/into called every time a member is used which is not worth the pain. Another advantage of traits: |
you don't need to create a new crate if the goal is that everyone switches to it and only uses it (realistically people won't). If that's what we are after, we'd be much better off saying that cgmath is the standard (I'd say euclid but I guess cgmath already has more users), and trying to convince other math libraries to make it easy to interoperate with cgmath. |
There's been discussions about providing standard data structures for common math types, but the interop question can also be addressed through traits. What would be the pros and cons of mint defining traits instead of data structures?
the kind of traits I am thinking about could be like:
Or could look very different, I don't know. I just want to get the discussion started. I have a feeling that I would prefer standard traits over standard data structures, probably because adding new data structures next to the ones I am already happily using feels redundant and cumbersome, but I'd need to explore this more to have an idea of how useful that would be.
The text was updated successfully, but these errors were encountered: