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

Allow accessing argument-less predicates on enum views without defining a custom view class #1419

Open
whitequark opened this issue Jun 25, 2024 · 1 comment

Comments

@whitequark
Copy link
Member

whitequark commented Jun 25, 2024

For example, this:

class SPIMode(amaranth.lib.enum.Enum, shape=2):
    Dummy = 0b00
    Swap  = 0b11
    Put   = 0b01
    Get   = 0b10

    @property
    def has_output(self):
        return self in (SPIMode.Swap, SPIMode.Put)

    @property
    def has_input(self):
        return self in (SPIMode.Swap, SPIMode.Get)

could have a default view class that acts as-if it was defined like this:

class SPIMode_View(...):
    @property
    def has_output(self):
        return Array(x.has_output for x in SPIMode)[self]

    @property
    def has_input(self):
        return Array(x.has_input  for x in SPIMode)[self]

(An actual implementation would use __getattr__, of course. This also means that a custom view class can override it easily.)

To avoid any ambiguity in types of arguments or unwanted combinational explosion, this would only occur for functions marked with @property. For all other functions a diagnostic would be shown indicating that a custom view class must be defined.

@whitequark
Copy link
Member Author

The big question for this proposal, besides "should we do this", is "what happens to values which don't have associated enum elements"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant