-
Notifications
You must be signed in to change notification settings - Fork 57
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
Improve generics usability for Array<T>
#264
Comments
I don't think they are just implementation detail. Generic Array trait bounds help with validating the input type statically at compile time. They also help establish what is expected input when a given set of functions are used inside a generic function. The other disadvantage of removing all these trait checks is ArrayFire functions (upstream library) calls will throw a runtime error when invalid inputs are passed to them.
I am not sure if that is something possible in rust, but I will look into it. I think a comprehensive trait impls for a limited set of combinations should solve the problem for some of these traits. But such brute trick won't help for all cases.
That is not a stable feature in rust yet and more importantly it is impractical to define trait aliases to all combinations of traits for user defined generic functions - there is no practical way to know what functions constitute a given user defined generic function.
I don't understand the question. I understand writing a generic function with so many trait bounds takes time to figure out the correct bound check. Let me see what can be done to improve the situation. |
Thanks for your reply 😃 I'm sorry, I didn't explain enough. You're right, traits are NOT implementation detail. Compile-time errors are much better than runtime errors. However, I think use arrayfire as af;
use num;
// alias-like trait (NOTE: This code does not require nightly.)
trait AfFloat:
num::Float
+ af::HasAfEnum<UnaryOutType = Self>
+ af::Convertable<OutType = Self>
+ af::ImplicitPromote<Self, Output = Self>
{
}
impl<T> AfFloat for T where
T: num::Float
+ af::HasAfEnum<UnaryOutType = T>
+ af::Convertable<OutType = T>
+ af::ImplicitPromote<Self, Output = T>
{
}
fn test_basic_op<T>(a: &af::Array<T>)
where
T: AfFloat + af::Fromf64,
af::Array<T>: std::ops::Neg<Output = af::Array<T>>,
{
...
} I wish |
Question 2. was about implementation of |
I was about to add a modified example of the code stub from your original description to show why I think such usage of traits:
I have seen the PR and will take a closer look at the generic changes and leave further feedback if required. Thank you for using and contributing to arrayfire-rust! |
|
Leaving this as a place holder issue to improve generic programming using rust wrapper. Any future user, kindly showcase your use case if facing issues. We will try to improve as more features of traits in rust stabilize. Note, I have added |
I encountered difficulty when writing generic functions using
Array<T>
e.g.After long fight with compile error, I found that
compiles and it's OK.
However, I feel that:
Convertable
andImplicitPromote
seems implementation detail and I don't want to write them every time when usingArray<T>
.T
should beConvertable<OutType=T>
automatically whenT: HasAfEnum
(i.e.T
is supported inarrayfire
).So I want:
AfScalar: HasAfEnum + ...
) which enables us ignoring implementation detail when usingArray<T>
in generic codeConvertable
), not implementation forf32
,f64
and other types individuallyWhat are your thoughts?
The text was updated successfully, but these errors were encountered: