Skip to content

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

Open
@whitequark

Description

@whitequark

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.

Activity

added this to the 0.6 milestone on Jun 25, 2024
whitequark

whitequark commented on Jun 27, 2024

@whitequark
MemberAuthor

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

removed this from the 0.6 milestone on Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @whitequark

        Issue actions

          Allow accessing argument-less predicates on enum views without defining a custom view class · Issue #1419 · amaranth-lang/amaranth