# VariadicList Not Working as Expected

**URL:** <https://forum.modular.com/t/variadiclist-not-working-as-expected/551>\
**Category:** Mojo\
**Tags:** discussion, feature-request, mojo-compiler, 24\_6, mojo-language-design\
**Created:** [February 10, 2025, 4:02pm UTC](https://forum.modular.com/t/variadiclist-not-working-as-expected/551 "2025-02-10T16:02:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![guna](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/guna/32/217_2.png) [@guna](https://forum.modular.com/u/guna)\
**Post date:** [February 10, 2025, 4:02pm UTC](https://forum.modular.com/t/variadiclist-not-working-as-expected/551/1 "2025-02-10T16:02:55Z")

</div>

Hey everyone!

I’m running into an issue with VariadicList initialization in Mojo [#3987](https://github.com/modular/mojo/issues/3987)

Here’s a minimal example of what I’m trying:

```mojo

fn main():
    alias shape_type = __mlir_type[`!kgen.variadic<`, Int, `>`]

    # Case 1: Direct initialization (works)
    var shape1 = __mlir_op.`pop.variadic.create`[_type = shape_type](Int(0), Int(0))

    # Case 2: Expected to prepopulate with 5 elements (doesn’t work)
    alias rank = 5
    var shape2 = __mlir_op.`pop.variadic.create`[_type = shape_type, size = rank](Int(0))

    # Case 3: Trying to append elements (no such op?)
    var shape3 = __mlir_op.`pop.variadic.create`[_type = shape_type]()
    for _ in range(rank):
        shape3 = __mlir_op.`pop.variadic.append`(shape3, Int(0)) # Doesn't seem to exist

    print(VariadicList(shape1). __len__ ()) # Output: 2
    print(VariadicList(shape2). __len__ ()) # Output: 0 (Expected: 5)
    print(VariadicList(shape3). __len__ ()) # Output: 0 (Expected: 5)

```

Is there a way to repeat elements when using pop.variadic.create for a VariadicList?

For example, instead of manually passing multiple values like this:

```mojo
var shape = __mlir_op.`pop.variadic.create`[_type = shape_type](Int(0), Int(0), Int(0), Int(0), Int(0))

```

Is there a way to dynamically repeat an element n times inside create itself, without manually listing them?

---

<div class="post-metadata">

**Author:** ![guna](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/guna/32/217_2.png) [@guna](https://forum.modular.com/u/guna)\
**Post date:** [February 22, 2025, 8:01pm UTC](https://forum.modular.com/t/variadiclist-not-working-as-expected/551/2 "2025-02-22T20:01:39Z")

</div>

actually i got the answer,

I assumed that **`pop.variadic.create`** could take a size argument, but I found the correct way to do it using **`pop.variadic.splat`**

```mojo
fn main():
    alias shape_type = __mlir_type[`!kgen.variadic<`, Int, `>`]

    # Case 1: Known arguments (works correctly)
    var shape1 = __mlir_op.`pop.variadic.create`[_type = shape_type](Int(0), Int(0))

    # Case 2: Prepopulate with a fixed number of elements (works)
    alias rank = 5
    var shape2 = __mlir_op.`pop.variadic.splat`[_type = shape_type, numElements = index(rank)](Int(0))

    print(VariadicList(shape1). __len__ ()) # Expected: 2, Output: 2
    print(VariadicList(shape2). __len__ ()) # Expected: 5, Output: 5

```

### **Findings**

1. **`pop.variadic.create`** only works with explicitly passed arguments.

- It does not support a `size` parameter for automatic repetition.

1. **`pop.variadic.splat`** correctly initializes a VariadicList with repeated values.

- The `numElements` argument controls the repetition.

1. There is no **`pop.variadic.append`** operation for VariadicList.

- Based on my research on the Mojo compiler symbols, **VariadicList is immutable after creation**.

---

<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:** [March 1, 2025, 8:02pm UTC](https://forum.modular.com/t/variadiclist-not-working-as-expected/551/3 "2025-03-01T20:02:05Z")

</div>

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