Runtime icon indicating copy to clipboard operation
Runtime copied to clipboard

Is it possible to create a struct instance by its name?

Open arturdev opened this issue 5 years ago • 7 comments

I'm looking some alternative to NSClassFromString but for structs :)

arturdev avatar Feb 11 '19 06:02 arturdev

Unfortunately it cannot as of now

wickwirew avatar Feb 12 '19 02:02 wickwirew

@wickwirew but is it theoretically possible?

arturdev avatar Feb 12 '19 12:02 arturdev

Potentially. You need to be able to get the type's metadata record from it's name. I do remember seeing a PR go into the compiler that allowed you to get it from the mangled name a while back. Other than that, as far as I know it's not. Would love to add it if it is though

wickwirew avatar Feb 12 '19 23:02 wickwirew

That will be just awesome if you add it!

arturdev avatar Feb 13 '19 06:02 arturdev

Hi @wickwirew. Have you got a chance to add this feature? :)

arturdev avatar Apr 28 '19 20:04 arturdev

Sorry but its just not possible. You can create a struct type from it's mangled name, but not just simply the name.

wickwirew avatar Apr 28 '19 23:04 wickwirew

if you can create struct Metadata,so you can create struct instance by the name,this is struct Metadata:

struct StructTypeDescriptor: TypeDescriptor {
    var flags: ContextDescriptorFlags
    var parent: Int32
    var mangledName: RelativePointer<Int32, CChar>
    var accessFunctionPtr: RelativePointer<Int32, UnsafeRawPointer>
    var fieldDescriptor: RelativePointer<Int32, FieldDescriptor>
    var numberOfFields: Int32
    var offsetToTheFieldOffsetVector: RelativeVectorPointer<Int32, Int32>
    var genericContextHeader: TargetTypeGenericContextDescriptorHeader
}

@wickwirew

jezzmemo avatar Jun 18 '21 03:06 jezzmemo