modular / modular/modular

[Feature Request] Parametrize ```__is__``` dunder

Open
#2,607 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement mojo mojo-repo postponed
Dominant language
Mojo
Stars
29.8k
Forks
3.2k
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the linked Mojo roadmap and the existing is and getitem entry points, then determine how parameterized type checks should work for Variant and PythonObject. Done means the behavior illustrated in the issue's examples is supported and validated by appropriate tests.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.