M
Modular12mo ago
Xcur

Can i have a list of structs with different parameters?

Lets say I have a struct like the one below, is it possible to have a list of structs that have different values inside their compile time parameters? This is just a sample struct, I know it's trivial and you wouldn't create a Name struct but this is just demonstrate the concept.
trait Proto(Stringable, CollectionElement):
fn print(self) -> None: ...

struct Name[DataSize: Int](Proto):
var name: String

fn __init__(inout self, owned name: String) -> None:
self.name = name^

fn __moveinit__(inout self, owned existing: Self) -> None:
self.name = existing.name^

fn __copyinit__(inout self, other: Self) -> None:
self.name = other.name

fn __str__(self) -> String:
var result: String = ""
for i in range(DataSize):
result += self.name[i]
return result

fn print(self) -> None:
print(self)
trait Proto(Stringable, CollectionElement):
fn print(self) -> None: ...

struct Name[DataSize: Int](Proto):
var name: String

fn __init__(inout self, owned name: String) -> None:
self.name = name^

fn __moveinit__(inout self, owned existing: Self) -> None:
self.name = existing.name^

fn __copyinit__(inout self, other: Self) -> None:
self.name = other.name

fn __str__(self) -> String:
var result: String = ""
for i in range(DataSize):
result += self.name[i]
return result

fn print(self) -> None:
print(self)
I could only implement it if I had DataSize as the same for each, but I would like to have a list of different DataSize name structs.
# this worked
var names = DynamicVector[Name[DataSize = 5]]()
# this does not work
var names = DynamicVector[Name]()
# this worked
var names = DynamicVector[Name[DataSize = 5]]()
# this does not work
var names = DynamicVector[Name]()
Is there a way to abstract it away, by using traits with all the necessary methods and then a wrapper struct that holds a name field with the trait as its type that I put each Name struct into? Then I could put the wrapper struct as a parameter in DynamicVector?
1 Reply
Xcur
XcurOP11mo ago
I've been attempting to solve this, but I'm having difficulty abstracting it away into a wrapper struct using traits. Here is the wrapper struct and sample code to try to run it that I added:
struct NameWrapper(Stringable, CollectionElement):
var name: Proto

fn __init__(inout self, owned name: Proto) -> None:
self.name = name^

fn __moveinit__(inout self, owned existing: Self) -> None:
self.name = existing.name^

fn __copyinit__(inout self, other: Self) -> None:
self.name = other.name

fn __str__(self) -> String:
return str(self.name)

fn main():

let nameString1: String = "John"
let nameString2: String = "Doe"

let name1 = Name[5](nameString1^)
let name2 = Name[3](nameString2^)

# unable to construct NameWrapper
let nameWrapper1 = NameWrapper(name1^)
let nameWrapper2 = NameWrapper(name2^)

var names = DynamicVector[NameWrapper](2)
names.append(nameWrapper1)
names.append(nameWrapper1)

print(len(names))

for i in range(2):
print(names[i])
struct NameWrapper(Stringable, CollectionElement):
var name: Proto

fn __init__(inout self, owned name: Proto) -> None:
self.name = name^

fn __moveinit__(inout self, owned existing: Self) -> None:
self.name = existing.name^

fn __copyinit__(inout self, other: Self) -> None:
self.name = other.name

fn __str__(self) -> String:
return str(self.name)

fn main():

let nameString1: String = "John"
let nameString2: String = "Doe"

let name1 = Name[5](nameString1^)
let name2 = Name[3](nameString2^)

# unable to construct NameWrapper
let nameWrapper1 = NameWrapper(name1^)
let nameWrapper2 = NameWrapper(name2^)

var names = DynamicVector[NameWrapper](2)
names.append(nameWrapper1)
names.append(nameWrapper1)

print(len(names))

for i in range(2):
print(names[i])
Can anyone help me with trying to solve this? I want to have a DynamicVector with different size Name structs.
Want results from more Discord servers?
Add your server