ObjectIdentifier of generic metatype

Hi Swift users!

Is it guaranteed that ObjectIdentifier's of the generic metatype with different generic arguments are different?

Basically, will this precondition always succeed (considering module boundaries, optimised builds, generic specialisation etc)?

struct Generic<T> {}

precondition(
  ObjectIdentifier(Generic<Int>.self)
  !=
  ObjectIdentifier(Generic<Double>.self)
)

And also, as ObjectIdentifier is basically a pointer, where will it point? I can see in a debugger that there are nonzero values. Is there some memory region for every generic and its arguments combination?

I am no expert, so please double confirm with the rest, given below is my understanding:

My Understanding:

  • ObjectIdentifier - Provides a unique identifier for comparing objects of a class (reference types not value types)
  • I think in your case you are comparing the type Generic<Int>.self represents the type Generic<Int>

Yes.

Generic<Int>.self != Generic<Double>.self and similarly ObjectIdentifier(Generic<Int>.self) != ObjectIdentifier(Generic<Double>.self)

It points to the type metadata of the type which is guaranteed to be unique between generic arguments.

2 Likes