-
Notifications
You must be signed in to change notification settings - Fork 764
Short syntax for impl generic params #2945
-
Today we have this syntax:
fn foo<T, impl TCopy: Copy<T>>;
It's not very ergonomic. We have a few suggestion for additional syntax to reduce it.
fn foo<T, Copy<T>>;
Here, if the generic param is a path with generics, then it is considered as an unnamed impl generic param.
A caveat of this is that traits with no generic will have to be specified like this: MyTrait<>, unless using the longer syntax
Same as the previous, but add impl before:
fn foo<T, impl Copy<T>>;
It's a bit more explicit but less ergonomic. And also avoide the MyTrait<> issue.
Somehow separate impls from other thing:
fn foo<T, S,
impls Copy<T>, Copy S>
This suggestion is kind of vague, sinec we are no certain about the concrete syntax in this suggestion. It does need to hold these requirements though:
i. Be able to specifiy the generic arguments explicitly when needed.
impl MyImpl <T, impls TC: Copy<T>> of MyTrait<T, impls TC> {...}
ii. Be able to name them when making an impl function for a trait function:
trait MyTrait{
fn foo<T, impls Copy<T>>();
}
impl MyImpl of MyTrait {
fn foo<T, impls NamedTC: Copy<T>>();
}
Other suggestion are welcome.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments 2 replies
-
I would suggest making Copy/Drop implementations implicitly generated if needed.
Other suggestions:
Rust-like 'bound' syntax:
fn foo<T: Copy + Print>(...) // or fn foo<T: Copy<T> + Print<T>>(...) // Can also be used like fn foo<T: Copy<Option<T>> + Print<Option<T>>>(...) // For naming: fn foo<T: TCopy(Copy<T>) + TPrint(Print<T>)>(...)
Rust-like 'where' syntax:
fn foo<T>(a: T) where Option<T>: Copy + Impl
The where syntax in particular easily supports naming things (example syntax here):
trait MyTrait { fn foo<T>(a: T) where Option<T>: Copy + Impl } impl MyImpl of MyTrait { fn foo<T>(a: T) where NamedTC(Option<T>): Copy + Impl }
Beta Was this translation helpful? Give feedback.
All reactions
-
These don't answer requirements i and ii though, right?
Beta Was this translation helpful? Give feedback.
All reactions
-
Don't they?
Requirement i as you wrote it:
impl MyImpl <T, impls TC: Copy<T>> of MyTrait<T, impls TC> {...}
With named bound:
impl MyImpl <T: TC(Copy<T>)> of MyTrait<T, impls TC> {...}
With where:
impl MyImpl <T> where T: TC(Copy<T>) of MyTrait<T, impls TC> {...}
Requirement ii I've made explicit above
Beta Was this translation helpful? Give feedback.
All reactions
-
For functions a hybrid of rust can be used (because Cairo doesn't have implicit self we must be explicit anyways)
fn foo<T>(a: SomeTrait<T> + Copy<T>, b: Option<T>) {}
Beta Was this translation helpful? Give feedback.