Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Better overload conditional conformance mechanics #3403

Open
1 task done
martinvuyk opened this issue Aug 21, 2024 · 0 comments
Open
1 task done
Labels
enhancement New feature or request mojo-repo Tag all issues with this label

Comments

@martinvuyk
Copy link
Contributor

Review Mojo's priorities

What is your request?

Better overload conditional conformance mechanics

The main problem is when making several overloads for a generic type, there needs to exist a way to set them in a hierarchy for their calling or the compiler should use the unconstrained signature last.

Why this should be a priority

Broadly speaking, this will benefit generic programming in Mojo a lot.

Going into specifics, In order to achieve things like what Chris mentioned in a community meeting a couple of weeks ago of decoupling the algorithms from the data structures, we need to be able to do this kind of overload conditional conformance and help the compiler resolve which signature to call when more than 1 coincide.

What is your motivation for this change?

I was trying to bring in the code for reversing Array with SIMD into List.

The constrained reverse function is currently ignored

struct List2[T: CollectionElement]:
    var data: UnsafePointer[T]

    fn __len__(self) -> Int:
        return 3

    fn reverse[A: DType](inout self: List2[Scalar[A]]):
        ...

    fn reverse(inout self):
        """Reverses the elements of the list."""

        var earlier_idx = 0
        var later_idx = len(self) - 1

        var effective_len = len(self)
        var half_len = effective_len // 2

        for _ in range(half_len):
            var earlier_ptr = self.data   earlier_idx
            var later_ptr = self.data   later_idx

            var tmp = earlier_ptr.take_pointee()
            later_ptr.move_pointee_into(earlier_ptr)
            later_ptr.init_pointee_move(tmp^)

            earlier_idx  = 1
            later_idx -= 1


fn main():
    var items = List("1", "2", "3")
    items.reverse()
    print(items.__str__()) # ['3', '2', '1']
    var items2 = List(1, 2, 3) 
    items2.reverse()
    print(items2.__str__()) # prints: "[3, 2, 1]" , should print "[1, 2, 3]" i.e. not be reversed

Any other details?

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

1 participant