Problem
The following code is currently not possible:
alias ComponentType = Copyable & Movable
trait ArchetypeIterator[*Ts: ComponentType]:
alias Element: Archetype[*Ts]
It can be worked around by specifying all variadic parameters as VariadicList[ComponentType]
, but this requires lots of refactoring and dismisses the whole point of having nice ergonomics around variadic parameters.
Workaround
alias ComponentType = Copyable & Movable
trait ArchetypeIterator:
alias ComponentTypes: VariadicList[ComponentType]
alias Element: Archetype[ComponentTypes]
Proposal
I know that (variadic) parameters are not in their final state and are considered sharp edges at the moment?!
I think variadic parameters for traits would make a great addition to the language, especially to enforce that a passed argument is an Iterator
for a specific Element
type:
# Possible with variadic parameters for Traits:
fn archetype_iter[*Ts: ComponentType, IterType: ArchetypeIterator[*Ts]](iter: IterType):
element = iter.__next__()
element.get_archetype_specific_attribute() # all good, because we can deduce that element must be of type Archetype[*Ts]
# Currently possible:
# V any Iterator allowed
fn any_iter[IterType: Iterator](iter: IterType):
element = iter.__next__()
element.get_archetype_specific_attribute() # wouldn't work, because element can be something else then Archetype[*Ts]
If traits got support for all parameter types (which probably would be the way to go to stay consistent), we could define a parametric Iterator
trait:
trait Iterator[Element: AnyType]:
fn __iter__(self, out element: Element): ...
...
This allows to require iterators for a specific Element
type when declaring a function. So the previous archetype_iter
function could be rewritten as:
fn archetype_iter[*Ts: ComponentType, IterType: Iterator[Archetype[*Ts]]](iter: IterType):
element = iter.__next__()
element.get_archetype_specific_attribute() # all good, because we can deduce that element must be of type Archetype[*Ts]
Question
Therefore I was wondering if you already have any thoughts/ideas regarding using variadic parameters in trait definitions?