M
Modular12mo ago
rdickert

Passing generics to DynamicVector

I'm trying to understand the traits system and am working with a toy list struct from this thread The original declaration (which works) starts with:
@value
struct list[T: CollectionElement](Sized):
var _internal_vector: DynamicVector[T]
@value
struct list[T: CollectionElement](Sized):
var _internal_vector: DynamicVector[T]
In the above I can declare a list of strings with list[String] I have tried to add the ability to print items in the list, but CollectionElements aren't accepted by str (errors with "no matching function in call to 'str'"), so I tried making a trait that inherits from both requirements:
trait ListElement(CollectionElement, Stringable):
...

@value
struct list[T: ListElement](Sized):
trait ListElement(CollectionElement, Stringable):
...

@value
struct list[T: ListElement](Sized):
This removes the compile error in my print method, but then I can no longer declare list[String]. In this declaration
fn read_strings_from_file(file_name: String) raises -> list[String]:
fn read_strings_from_file(file_name: String) raises -> list[String]:
the compiler errors with "'list' parameter #0 has 'ListElement' type, but value has type 'String'". But it seems like String should satisfy my new trait since it satisfied CollectionElement and should certainly satisfy Stringable. Am I doing something wrong, or is this a current bug/limitation of Mojo?
GitHub
[BUG]: Mojo is getting confused in 0.6.0 with overloading and slice...
Bug description The compiler is choosing the wrong overload, making it break the program at runtime. Steps to reproduce Consider the following code: @value struct list[T: CollectionElement]: var _i...
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server