-
Notifications
You must be signed in to change notification settings - Fork 148
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
Extension of num::Zero ? #440
Comments
I think it's reasonable, but it should be clear that this can't help any code that only uses Outside vector-like things, another example I thought of was a dynamic modular-arithmetic type, where the modulus is another field that should be provided on construction. Although depending on how that works with mixed moduli, such a type might be happy enough with |
Yes that is the case the impl<T> num::Zero for T
where
T: ZeroFrom<()>,
{
fn zero() -> Self {
<Self as ZeroFrom<()>>::zero_from(&())
}
}
But for libraries such as |
For the sake of compatibility, I would write that blanket impl the other way: impl<T: Zero> ZeroFrom<()> for T {
fn zero_from(_: &()) -> Self {
T::zero()
}
} I think it would make sense for crates like |
When we actually implement it, we might do it like this. I just wanted to emphasize that in principle
I did this for my own crate and implemented the new impl<T, S> ZeroFrom<S> for T
where
T: num::Zero
{
fn zero_from(_: &S) -> Self {
Self::zero()
}
} Now we can not manually implement But it could be useful to want this type of blanket implementation instead of fn some_fun<F, X, Y, S>(a: Option<F>, x: X, y: Y, s: &S) -> X
where
X: core::ops::Mul<F, Output=X>,
X: ZeroFrom<S>,
X: core::ops::Add<Y, Output=X>,
Y: ZeroFrom<S>,
{
if let Some(a) = a {
x * a + y
} else {
X::zero_from(s) + y
}
} Now let's say that the type |
I think it would make more sense for |
So if Maybe my concern is also unfounded regarding the blanket impl and it could be enough to do |
If you're going to add extra |
I actually think that the most idiomatic way to do it would be impl<T: num::Zero> ZeroFrom<()> for T {
fn zero_from(_: &()) -> Self { Self::zero() }
} and thus treat |
Setting
I am designing a crate which performs numerical calculations but abstracts over the underlying types in question such that it can be used with static, dynamic, Vectors, Matrices, etc. A typical function in this context may look like this:
It is obvious for me to test very frequently with types from the popular nalgebra crate.However the situation becomes problematic when dealing with dynamically sized objects which do no implement the
num::Zero
trait. These objects do implement the generalFrom
trait but this trait carries no information about whether the object is zero or not. They also implementDefault
but this initializes a 0x0-dimensional matrix with no entries.Question
So my question is: Is it reasonable and possible to generalize the existing
num::Zero
trait to also include constructor methods based on external input?Nalgebra already provides the
::zeros(nrows, ncols)
methods to construct empty matrices but they do not fall under any trait. In the future, we will probably see many more crates for abstractions of vectors, tensors (such as ndarray) etc. (possibly on the GPU as well).Example
An initial attempt from me looked like this:
See this playground example.
The text was updated successfully, but these errors were encountered: