Skip to content

[Feature Request] [stdlib] [mojo-lang] A way to initialize a type from an arguments list at the function callsite #5935

@martinvuyk

Description

@martinvuyk

Review Mojo's priorities

What is your request?

I mainly want to be able to know how many arguments are being passed into a function as requested in #4144. But since we can't modify the behavior of VariadicList itself, then I request that we maybe allow other types like VariadicPack to be built from an argument list. An example:

from testing import assert_equal, assert_false

comptime Several[
    elt_is_mutable: Bool,
    origin: Origin[mut=elt_is_mutable],
    //,
    T: AnyType,
    times: Int = 1,
    is_owned: Bool = False,
] = VariadicPack[
    elt_is_mutable=elt_is_mutable,
    origin=origin,
    is_owned,
    type_of(T),
    *Variadic.splat[T, times],
]

comptime SeveralTs[
    elt_is_mutable: Bool,
    origin: Origin[mut=elt_is_mutable],
    //,
    element_trait: type_of(AnyType),
    is_owned: Bool,
    *element_types: element_trait,
] = VariadicPack[
    elt_is_mutable=elt_is_mutable,
    origin=origin,
    is_owned,
    element_trait,
    *element_types,
]


def main():
    def test_trivial_type(a: Several[Int]):
        @parameter
        for i in range(a.__len__()):
            assert_equal(a[i], 0)

    def test_non_trivial_type(a: Several[String]):
        @parameter
        for i in range(a.__len__()):
            assert_equal(a[i], "")

    def test_trait(a: SeveralTs[Boolable]):
        @parameter
        for i in range(a.__len__()):
            assert_false(a[i])

    test_trivial_type(0, 0, 0)
    test_non_trivial_type("", "", "")
    test_trait("", 0, False, [])

maybe could solve this doing something similar to how we deal with initializer list or literal collection initializers?

What is your motivation for this change?

#5919 (comment) and several other places.

Any other details?

I also think that if we solve this correctly we'll finally avoid the need of having 2 function signatures for things that receive a VariadicPack per se, and others that receive arguments in the function call itself

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs TriageIssue needs to be routed/triaged to a particular team stillenhancementNew feature or requestmojoIssues that are related to mojo

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions