ModularM
Modular2y ago
3 replies
MyriadColors

How to initialize an empty List[String]

What Im trying to do:

from python import Python

fn add(a: Int, b: Int) -> Int:
    return a + b

fn sub(a: Int, b: Int) -> Int:
    return a - b

fn mul(a: Int, b: Int) -> Int:
    return a * b

fn safe_div(a: Int, b: Int) -> Float32:
    return a / b

fn main() raises:
    var user_input = Python.evaluate("input('Enter [verb] [args]: ') ").split(" ")

    var verb: String = ""
    var args: List[String]

    for i in range(0, len(user_input)):
        if i == 0:
            verb = user_input[0]
        else:
            args.append(user_input[i])


Problem: when compiling:
/home/pedrot/Desktop/Coding/mojo/learn/main.mojo:25:24: error: use of uninitialized value 'args'
args.append(user_input[i])
^
/home/pedrot/Desktop/Coding/mojo/learn/main.mojo:19:5: note: 'args' declared here
var args: List[String]
^
mojo: error: failed to run the pass manager

But when I try:

var args: List[String] = []


It also complains:
/home/pedrot/Desktop/Coding/mojo/learn/main.mojo:19:30: error: cannot implicitly convert 'ListLiteral[]' value to 'List[String]' in 'var' initializer
var args: List[String] = []
^~
mojo: error: failed to parse the provided Mojo source module

THank you.
Was this page helpful?