Attempt FALLBACK_SET/GET on failure to find field via SET/GET #440
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.
I made it possible to fallback to
Protocol::INDEX_GET
Protocol::FALLBACK_GET
withStaticString
/String
key on failure to find a field viaProtocol::GET
, the same applies toProtocol::INDEX_SET
Protocol::FALLBACK_SET
andProtocol::SET
.Reasoning
Sometimes it is desired for user readability to pretend a specific custom type has fields and instead accessing the data from elsewhere such as from a
HashMap
or another collection.possible use cases is having types with dynamic layouts while having rules to their usage such as locking the layout once created.
it also allows custom types to be dynamically extended by the scripter while keeping the benefits of being a static
Shared<AnyObj>
but not seeming like a collection which[]
accesses give the impression of.Benchmarks
Note
Due to the tiny footprints of the functions, the differences are very hard to see if any and can be improved with a faster hashmap regardless but honestly values these small are easily within margin of error.
Before
After
Closes
#381 #263
Notes
Regarding the changes done to this PR, I do wonder if FALLBACK_* is preferred choice compared to META_* or META_FIELD_* since in most script languages I have used, META usually implies it does it first/instead not after, which although being a option would be a fine choice.
I do like the advantage of the current design being that it prioritises actual fields first.
I do wonder if it was changed to use the 'meta' getter/setter first if it would be plausible to pass in the original field getter and setter functions so u could do this:
This approach however would have overhead and although could be faster for types which only want 'meta' fields I definitely feel it would cost for classic field get/sets which would be a waste and a shame and it isn't the most attractive solution regardless.
Another possible solution could be explicitly declaring a type as having 'meta' fields and which order to prioritise finding them such as
MetaFieldAccess::First
MetaFieldAccess::Last
,MetaFieldAccess::Always
and finallyMetaFieldAccess::Never
(default) which would be drastically cheaper and simpler than it being passed in as a argument to the meta function which in my opinion reduces the understandability for implementors.The enum system could even be applied to the
Any
derive macro as an attribute to make it easy to implement and recognise by looking at types.