Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Short syntax for impl generic params #2945

spapinistarkware started this conversation in General
Discussion options

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.

You must be logged in to vote

Replies: 2 comments 2 replies

Comment options

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
}
You must be logged in to vote
2 replies
Comment options

These don't answer requirements i and ii though, right?

Comment options

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

Comment options

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>) {}
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /