# How to build an extendable parametric struct

**URL:** <https://forum.modular.com/t/how-to-build-an-extendable-parametric-struct/347>\
**Category:** Mojo\
**Tags:** discussion, feature-request, 24\_6\
**Created:** [December 18, 2024, 3:12pm UTC](https://forum.modular.com/t/how-to-build-an-extendable-parametric-struct/347 "2024-12-18T15:12:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![samufi](https://avatars.discourse-cdn.com/v4/letter/s/d2c977/32.png) [@samufi](https://forum.modular.com/u/samufi)\
**Post date:** [December 18, 2024, 3:12pm UTC](https://forum.modular.com/t/how-to-build-an-extendable-parametric-struct/347/1 "2024-12-18T15:12:51Z")

</div>

I want to build a package with a struct that relies on some element type parameters but can be extended to work with additional element types as well. Suppose this is the basic structure:

```mojo
struct MyStruct[*Ts: AnyType]:
   ...

struct MyPowerfulStruct:
   var _data = MyStruct[MyType1, MyType2, MyType3]
   ...

```

I would like to achieve that package users can add further types to `MyPowerfulStruct`. Are there design concepts for this kind of problem? Can this be solved with Mojo at the moment?

My first thought was to do something along the lines of the following:

```mojo

struct MyPowerfulStruct[*Ts: AnyType]:
   var _data = MyStruct[MyType1, MyType2, MyType3, *Ts]
   ...

```

Will this be possible in the near future? Is there any other / more elegant way to achieve this?

---

<div class="post-metadata">

**Author:** ![clattner](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/clattner/32/201_2.png) [@clattner](https://forum.modular.com/u/clattner)\
**Post date:** [December 30, 2024, 9:49pm UTC](https://forum.modular.com/t/how-to-build-an-extendable-parametric-struct/347/2 "2024-12-30T21:49:46Z")

</div>

We don’t have any planned support for “inheritance” of structs, but we should be able to do what you’re talking about with containment like that. It sounds like you’re looking for better support for variadic types?

The major challenge we have right now is that we don’t support nice variadic unpacking - there are a variety of workarounds, but I agree it is not very beautiful. For example, you can see how various types build on top of heterogenous varadic types in the standard library: Tuple is the canonical example, and the VariadicPack type in the standard library is what is used to implement the syntax.

---

<div class="post-metadata">

**Author:** ![samufi](https://avatars.discourse-cdn.com/v4/letter/s/d2c977/32.png) [@samufi](https://forum.modular.com/u/samufi)\
**Post date:** [December 31, 2024, 1:41pm UTC](https://forum.modular.com/t/how-to-build-an-extendable-parametric-struct/347/3 "2024-12-31T13:41:30Z")

</div>

> [@clattner](#):
>
> Tuple

Hmm. “Not beautiful but works” would be totally fine for me at the moment. Nonetheless, though I think I have looked at several places of the standard library and tried out quite a couple of things, I did not find _any_ way to do some of the things I need. In particular, this are

- concatenation of variadic arguments / packs
- conversion between different variadic types (e.g. variadic list to variadic pack)
- parametrizing based on multiple variadic arguments (such as below:)

```mojo
struct S1[*Ts: AnyType]:
    pass

struct S2[*Ts: AnyType]:
    pass

sruct Connector[Ts1, Ts2]:
    var s1: S1[*Ts1]
    var s2: S2[*Ts2]

```

To me these things look like they should not be too difficult to implement in principle (as I assume that they the behaviour is somewhat clearly defined, maybe with exception of the last case, which would require support for some packaging of types), but I cannot say I can accurately assess the difficulty of implementing this. Therefore my question.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/2751e0fbdc595a99718b216730957e9db4448cfd.jpeg) [@system](https://forum.modular.com/u/system)\
**Post date:** [June 29, 2025, 1:42pm UTC](https://forum.modular.com/t/how-to-build-an-extendable-parametric-struct/347/4 "2025-06-29T13:42:23Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
