#Error with method append with string

1 messages · Page 1 of 1 (latest)

warm wing
#

When im try use List for string a get error

    let list = List[String]()
    list.append("123")```

Error:
```/home/asifar/http/main.🔥:38:16: error: no matching function in call to 'append': 
    list.append("123")
    ~~~~~~~~~~~^~~~~~~
list.mojo:133:5: note: candidate not viable: method argument #0 cannot bind generic !mlirtype to memory-only type 'String'
    fn append(inout self, value: T):
    ^
list.mojo:139:5: note: candidate not viable: method argument #0 cannot be converted from 'StringLiteral' to 'List[String]'
    fn append(inout self, list: List[T]):
    ^
/home/asifar/.modular/pkg/packages.modular.com_mojo/bin/mojo: error: failed to parse the provided Mojo```
warped fog
#

Hey, the error message is a bit unclear, but it means that you can only use simple @register_passable structs in lists (which String is not)
You can use a StringLiteral which is immutable but may be enough for what you're trying to achieve.

warm wing
warped fog
#

append is a mutating method that must modify the underlying storage, but you declared the variable as let (immutable)
You have to declare the list as a var

warm wing
#

Thanks It works)

warped fog
#

If you still want a list that you can't modify later you can initialize it upfront

let list: List[StringLiteral] = ["a", "b", "c"]
warm wing
warped fog
#

As far as I know you can't have pointers in a list yet either so I doubt it
Not sure why considering it has no destructor but Pointer is not @register_passable

daring nacelle
#

how to get rid of this warning

warped fog
daring nacelle
#

_= is it the only way
here the return values are just useless

warped fog
#

This is not a problem unique to Mojo when it comes to this kind of interoperability