#How do I just copy all type parameters for verbatim use in an enclosing type?

8 messages · Page 1 of 1 (latest)

slim estuary
#

For example if I have an existing type

Foo<'bar, B: Baz, W: Wut = ()> { ... } 

and I have a struct I want to create

struct MyType<?> {
    foo: Foo<?>,
}

What can I do to scrape in all the ? and just use it verbatim in Foo? Is that a thing?

uneven depot
#

You can't. A type's generic list can't be dependent on another type's list; you need to specify them explicitly

slim estuary
#

I can't quite figure out which direction the "dependant" goes in your answer. Do you mean that the ? in Foo<?> must be explicit? Like Foo<ConcreteType>?

uneven depot
#

No. You can't make MyType's generic list inherit Foo's generic list, or the other way around: they have to be declared separately

#

You have to manually do this: ```rust
struct MyType<'bar, B: Baz, W: Wut = ()> {
foo: Foo<'bar, B, W>,
}

slim estuary
#

ooh. ok! that makes sense. it would make no sense for the W: Wut = () to appear inside the struct. I guess it's analogous to a function's parameters (in the declaration) vs arguments (in the body).

tender rock
#

depending on what you're doing, it might be possible to work with a

struct MyType<F> {
    foo: F,
}

and then you only need to mention the Foo type and its parameters where they are relevant (such as in impl blocks), rather than in every single use of MyType

uneven depot
#

That too ^