M
Modular12mo ago
❤🔥

Error with method append with string

When im try use List for string a get error
fn main() raises:
let list = List[String]()
list.append("123")
fn main() raises:
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
/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
11 Replies
TeamPuzel
TeamPuzel12mo ago
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.
❤🔥
❤🔥12mo ago
/home/asifar/http/main.:fire::38:16: error: invalid mutation of immutable value 'list.capacity'
list.append("123")
^
/home/asifar/http/main.:fire::37:5: note: 'list' declared here
let list = List[StringLiteral]()
^
/home/asifar/.modular/pkg/packages.modular.com_mojo/bin/mojo: error: failed to run the pass manager
/home/asifar/http/main.:fire::38:16: error: invalid mutation of immutable value 'list.capacity'
list.append("123")
^
/home/asifar/http/main.:fire::37:5: note: 'list' declared here
let list = List[StringLiteral]()
^
/home/asifar/.modular/pkg/packages.modular.com_mojo/bin/mojo: error: failed to run the pass manager
I made StringLiteral, and already another error.
TeamPuzel
TeamPuzel12mo ago
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
❤🔥
❤🔥12mo ago
Thanks It works)
TeamPuzel
TeamPuzel12mo ago
If you still want a list that you can't modify later you can initialize it upfront
let list: List[StringLiteral] = ["a", "b", "c"]
let list: List[StringLiteral] = ["a", "b", "c"]
❤🔥
❤🔥12mo ago
Is there a way to create a list for a String type on the heap?
TeamPuzel
TeamPuzel12mo ago
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
Tanvir
Tanvir12mo ago
how to get rid of this warning
No description
TeamPuzel
TeamPuzel12mo ago
I don’t think this is related so this should be a different question You’re not using the potential return values, discard them by assigning these to an underscore with _ =
Tanvir
Tanvir12mo ago
_= is it the only way here the return values are just useless
TeamPuzel
TeamPuzel12mo ago
You’re interacting with Python, an untyped, dynamic language, so there’s no way to be sure it won’t actually return anything This is not a problem unique to Mojo when it comes to this kind of interoperability You seem to already be discarding return values in your screenshot so I’m not sure what the issue is
Want results from more Discord servers?
Add your server