Add enum variant index to GetPath#23137
Open
IronGremlin wants to merge 5 commits intobevyengine:mainfrom
Open
Conversation
This is the dumbest possible version of this. An optional validation for enum variant index. Knowledge of which vidx you have is not enforced, but if supplied, it is validated.
Provide syntatical enforcement for variant index access via GetPath. Update docs.
Contributor
|
It looks like your PR is a breaking change, but you didn't provide a migration guide. Please review the instructions for writing migration guides, then expand or revise the content in the migration guides directory to reflect your changes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Objective
Currently, the machinery provided by Bevy's
GetPathtrait is agnostic about which enum variant it provides access to.This limitation prevents users from disambiguating between identically named fields on variant structs (ehh...) or indices on variant tuples (EG, there's no way to disambiguate access between say,
Result::OkorResult::Err).Historically, this ability to disambiguate hasn't been necessary to support common reflection cases, as we can generally assume that under reflection, you have an instance available to reflect, and can therefor interrogate the structure of the instance if you really need to.
However, as
The Editor(tm) fast approaches, a new category of use-cases forbevy_reflectare being uncovered -They include such cases as:
AccessPathmay need to be constructed without access to an instance of a typeCustomAttribute(or similarly stored future concepts) data to decorate editor functionality at runtime - allowing theTypeRegistryor similar, editor/inspector-specific resource to play host to arbitrary field level dataImagine, for example, some resource -
where a tool author might be able to selectively override inspector widget construction for their project.
Today, we can't build that, because
AccessPathcan facilitate arbitrary structural access for an instance, but the inability to discriminate between enum variants means it can't express arbitrary structural access for a type.Solution
Implement a minimal adjustment to
GetPathto support discrimination between enum variant indices.Testing
Changes necessary to support this feature were small enough that the existing test suite was able to provide adequate coverage with only minimal additions.
Showcase
Example of accessing an enum under reflection today:
Example of proposed change:
Alternatives and Disclaimers
I do not understand the current design choice.
I'm not sure why we chose not to do this originally.
It is very possible I have overlooked some vital use-case that my change would invalidate or over-complicate.
This syntax is kinda gross
I'm not really a huge fan of this syntax.
The current implementation of
Accessrelies on being able to jam theTokenenum into au8- I did what seemed sensible with the token space available. It seems extremely plausible that better ideas exist here.We could support this but not require it
I started with an even smaller version of this change that would make specifying the enum variant index optional - the syntax would work largely as is, but you could optionally supply an enum variant index and
Accesswould validate it for you if it found one.I'm still not entirely confident that's not a superior option, but I ended up doing this instead simply because I kind of stalled out trying to write docs that adequately explained what good optional validation would be to anyone without info-dumping my editor design manifesto into a doc comment.
We could just not do this.
We could also just re-implement most of this machinery in a more specifically targeted fashion for Inspector/Editor use instead.
For my part, I believe expanding this concept very slightly to support adjacent use-cases makes more sense than re-implementing this elsewhere, but I also feel I am largely ignorant of the full scope of the design goals for
bevy_reflect, and could certainly be wrong.