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] Parametrize __is__ dunder #2607

Open
1 task done
rd4com opened this issue May 10, 2024 · 1 comment
Open
1 task done

[Feature Request] Parametrize __is__ dunder #2607

rd4com opened this issue May 10, 2024 · 1 comment
Labels
enhancement New feature or request mojo-repo Tag all issues with this label

Comments

@rd4com
Copy link

rd4com commented May 10, 2024

Review Mojo's priorities

What is your request?

Hello,

that feature improved __getitem__ by letting take indexes as parameters (Variant[Int,Bool](1)[Int]),

__is__ might get an improvement by it too, here are some examples:

 

Example for variant:

struct Variant[*Ts:CollectionElement]():
    fn __is__[
        other: CollectionElement
    ](self)->Bool: 
        return self.isa[other]()

fn main():
    var x = Variant[Int,Bool](1)
    if x is Int: print(x[Int])
    if x is Bool: print(x[Bool])

And for PythonObject:

x = PythonObject(1)
if x is Int: print(int(x))
#if x is List
#if x is Tuple

What is your motivation for this change?

Variant is a nice type that can seem weird for python users, this could make it more user-friendly.

it could also ameliorate any user-created structs in a super user-friendly manner.

Any other details?

No response

@rd4com rd4com added enhancement New feature or request mojo-repo Tag all issues with this label labels May 10, 2024
@gabrieldemarmiesse
Copy link
Contributor

In Python, the operator is allows the programmer to check the identity of an object, in other words, it allows someone to check that two objects have the same address in memory. While this proposal will make the Variant struct have a nicer api, it may conflict in the future when is will be used to check the memory addresses of references.

Maybe let's hold on to this syntax sugar until we know how is will be used exactly with references?

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

2 participants