ModularM
Modular2y ago
8 replies
Josiah

Difficult Trait Conformance

I have been using mojo nightly mojo 2024.8.305 (f99b2e40). I need someone's help on why this doesn't work:
trait IterDataPipable:
    fn __next__[T: CollectionElement](ref[_] self) -> Optional[T]: pass

@value
struct ListDatapipe[T: CollectionElement](IterDataPipable[T]): 
    var src: List[T]
    var index: Int

    fn __init__(inout self, src: List[T]):
        self.src = src
        self.index = 0

    fn __init__(inout self, src: List[T], index: Int):
        self.src = src
        self.index = index

    fn __iter__(self) -> Self: return self

    fn __next__(ref[_] self) -> Optional[T]:
        self.index += 1
        if self.index == len(self.src) + 1:
            return Optional[T](None)

        return Optional[T](self.src[self.index - 1])

    fn __len__(self) -> Int: return len(self.src) - self.index

When running:
    var numbers = List(1,2,3,4,5)
    var pipe = ListDatapipe(numbers)
    # var pipe2 = AnotherPipe(pipe)
    # additional pipes
    var n = pipe.__next__()

It keeps saying ListDatapipe doesnt conform to IterDataPipable. I've tried a bunch of different ways of trying to make this work, but none of them work well.
I'm trying to get a generic IterDataPipable working so I can chain. Tried getting help from https://discordapp.com/channels/1087530497313357884/1269718914208759870 kapa. I'm convinced its an issue with https://github.com/modularml/mojo/issues/3215
Was this page helpful?