ModularM
Modular17mo ago
4 replies
Hasan Yousef

How can I convert String to StringLiteral

I understood that str() is required to convert things to String like (StringLiteral, Int, Bool, ...), but is there a way to covert String to LiteralString.

As part of learning Mojolang I'm trying the below code, then this conversion issue poped up.
from collections import List
from sys.ffi import external_call

alias system = external_call["system", Int, StringLiteral] 

fn run_commands(arg0: String, args: List[String]) -> Int:
    var command: String = arg0
    for arg in args:
        command += " " + arg[]
    return system(command) # invalid indirect call: argument #0 cannot be converted from 'String' to 'StringLiteral'

fn system2(arg0: String, args: List[String]) -> Int:
    var command: String = arg0
    for arg in args:
        command += " " + arg[]  # Access the element inside the list
    return external_call["system", Int, String](command) 

fn main():
    # var args = List[String]("kill", "-9", "$(lsof -t -i :8000)")
    var args = List[String]("ls")
    _ = run_commands(args[0], args[1:])
    _ = system2(args[0], args[1:])
Was this page helpful?