Skip to content
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

[Feature Request] Allow substitution of child traits for parent traits #2413

Open
1 task done
lsh opened this issue Apr 26, 2024 · 2 comments
Open
1 task done

[Feature Request] Allow substitution of child traits for parent traits #2413

lsh opened this issue Apr 26, 2024 · 2 comments
Labels
enhancement New feature or request mojo Issues that are related to mojo mojo-repo Tag all issues with this label

Comments

@lsh
Copy link
Contributor

lsh commented Apr 26, 2024

Review Mojo's priorities

What is your request?

If a function takes variadic arguments bound by a trait, then a sub-trait should be allowed to substitute that trait.

What is your motivation for this change?

The following code should be possible with this change:

trait StringableCollectionElement(CollectionElement, Stringable):
   …

struct A[*Ts: StringableCollectionElement]:
    var tup: Tuple[Ts]

Any other details?

No response

@lsh lsh added enhancement New feature or request mojo Issues that are related to mojo labels Apr 26, 2024
@StandinKP
Copy link

Where would this be used?

@lsh
Copy link
Contributor Author

lsh commented Apr 27, 2024

@StandinKP right now tuples holding variadic types aren't particularly useful when trying to work in a generic way. Suppose you have this:

@value
struct MyStruct[*Ts: CollectionElement]:
    var tup: Tuple[ts]

What can you actually do with this? Since the only thing you know about each type is that they conform to CollectionElement, that's the only thing you can use those types for within MyStruct. Basically, all you can do is store, move, and copy the values.

If you tried to add a method:

struct MyStruct[*Ts: CollectionElement]:
    # ...
    fn print_first_value(self):
        print(self.tup[0])

this would fail to compile, since you don't know that each element conforms to Stringable.

@ematejska ematejska added the mojo-repo Tag all issues with this label label Apr 29, 2024
@lsh lsh changed the title [Feature Request] Allow substation of child traits for parent traits [Feature Request] Allow substitution of child traits for parent traits May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request mojo Issues that are related to mojo mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

3 participants