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

Conditional Trait Conformance #2308

Closed
1 task done
Moosems opened this issue Apr 15, 2024 · 5 comments
Closed
1 task done

Conditional Trait Conformance #2308

Moosems opened this issue Apr 15, 2024 · 5 comments
Labels
enhancement New feature or request mojo Issues that are related to mojo mojo-lang Tag for all issues related to language. mojo-repo Tag all issues with this label

Comments

@Moosems
Copy link

Moosems commented Apr 15, 2024

Review Mojo's priorities

What is your request?

I believe it would be nice for there to be conditional trait conformance in structs. This could be achieved through a a decorator that is passed static tuples that contain a type, the trait, and the struct value that needs to conform to this.

Example use case:

@value
struct SpecialStruct[T]:
    var x: T
    var y: T
    @conditional((T, Stringable, Self.x))
    fn __str__(self) -> String raises:
        # raises in case T does not conform
        return self.x

    @conditional((T, Intable, Self.x), (T, Intable, Self.y))
    fn mult_vals(self) -> Int raises:
        # tuple ao you can use multiple struct variables
        return self.x * self.y

What is your motivation for this change?

It would allow for more flexibility and be nice when a programmer knows if something will work.

Any other details?

Could be implemented in other ways but the idea/concept is still pretty important

@Moosems Moosems added enhancement New feature or request mojo Issues that are related to mojo labels Apr 15, 2024
@ematejska ematejska added the mojo-lang Tag for all issues related to language. label Apr 15, 2024
@Moosems
Copy link
Author

Moosems commented Apr 15, 2024

Some alternate ideas for syntax include:

trait Purchaseable:
    fn buy(): …

struct Array[T]:
    fn buy() if conforms[T, Purchaseable] raises: …

Or

trait Purchaseable:
    fn buy(): …

struct Array[T: AnyType](Purchaseable if T): …

Or (by far my least favorite as it requires another set of indentation)

struct Optional[T]:
    #
    if conforms[T, CollectionElement]:
        fn blah():
            pass

With the last one there should, in my opinion, also be a way to not make another layer of indentation. In my experience you almost never need more than three layers of indentation and excessive layers are ugly. This is why many opt for guard clauses instead of layered ifs. You could say that it's because of the function definition, and thats true, but there's ways to not need the if statement or put in it a way that doesn't need more indentation

@gabrieldemarmiesse
Copy link
Contributor

I believe this is related to #1876

@Moosems
Copy link
Author

Moosems commented Apr 16, 2024

Yes, thank you, closing in favor of #1876

@Moosems Moosems closed this as completed Apr 16, 2024
@Moosems
Copy link
Author

Moosems commented Apr 16, 2024

What do you think of the proposed syntax, if I may @gabrieldemarmiesse?

@gabrieldemarmiesse
Copy link
Contributor

I have no opinion on the syntax, everything is fine by me

@ematejska ematejska added the mojo-repo Tag all issues with this label label May 6, 2024
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 Issues that are related to mojo mojo-lang Tag for all issues related to language. mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

3 participants