#How to type a slice of slices?

18 messages · Page 1 of 1 (latest)

brittle root
#

I tried

type Archetype[T] struct {
    world World
    signature = []uint64
    components = [][]T
    edges = map[uint64]Archetype
}

but it didnt work. idk what im doing wrong and google didnt avail me of this knowledge

peak galleon
#

no =, and you need to give T a type constraint, even if it's any

brittle root
brittle root
#

so like this?

peak galleon
#

sure

brittle root
# peak galleon sure

so it made me add any like this

type Archetype[T any] struct {
    world      World
    signature  []uint64
    components [][]T
    edges      map[uint64]Archetype[any]
}
#

does the any hear mean the archetype will be of type any or that it can take any archetype

#

cause i really want the latter

peak galleon
#

the former

#

do you want T?

brittle root
#

each archetype will have a set of components, each of a generic type

#

hmmm. i think i did that wrong then

brittle root
# peak galleon the former

so let me explain the purpose of this, that might help.

I want to create an archetypal ecs, and this struct represents all the entities that own the same types of components.

ie, if we had an archetype of entities that had string and int64:

  • the signature would have those 2 component ids
  • the components would be an array with a slice of int64 and a slice of string
#

should i perhaps use a map for the components instead of an array?

mild canyon
#

If you calll components [][]any, whenever you refer to the components through the Archetype, at runtime, the program will not know which are the strings and which are the int64's at that point. You would have to cast the any as an int or a string and somehow keep track of which is which. If the elements in the two slices are a key value pair then you'd probably wanna use a map[string]int64 or something like that. But also you might already know that. Sorry if this doesn't help