Currently what's implemented is the type of the instance that this is on. I.e.
struct A {
@CustomRuntimeAttribute
var x: String
}
In this case I can actually provide you a value of (String.self, CustomRuntimeAttribute?)
, so the API could be edited to look like [(Any.Type, T?)]
(or something wrapped in a struct with a proper name).
Yes, querying in the other direction is something a lot of folks have been asking for and I imagine we'll want to alter the implementation a bit to allow for efficient querying the other way. I would love if this interacted with the proposed API in the reflection pitch to allow for say Type.attributes
or Field.attributes
to get all the attributes on a specific type or a specific field.
These attribute initializers are only called when you request them via the getAllInstances
API. Also, only the specific attribute's initializers are called. For example, if you have 2 custom runtime attributes @A
and @B
and you ask for @B
's instances, we'll only initialize those attributes whose type is B
and not any of the A
ones.
The getAllInstances
method is provided for you via the proposed Reflection
module ([Pitch] Reflection). You are expected to implement your own attachedTo:
inits to constrain what type of declarations your attribute can be applied to.
So we do both. On platforms like Linux and Windows, we need to scan each section every time for every attribute. On platforms like Darwin, we can scan each section once, cache the results, and for new images loaded at runtime we'll be notified of new sections and only have to scan those and cache the results.
Yes, the API would return [A, B, C]
and if you only wanted to consume C
you'd need to do bookkeeping to figure what you've already seen.