Is there a way to differentiate Swift types and C types at runtime?

Hi,

I'm writing a program that heavily relies on types declared in C. I was wondering, whether there is a way to differentiate C types and Swift types.

Let's say I want to bind a pointer to a type but I only want to do it, if the type was declared in C and throw exception otherwise. So far the only difference I have noticed is, that all the C types belong to a module called __C.

To be more specific, I don't mind Swift types per-se. I just need to be sure, that the layout is done "the C way" and no calls to Swift runtime are made (so no retains etc.). At this time, the only thing stopping a programmer from invoking the code with incorrect type is a warning in documentation. Which seems insufficient to me when the result of incorrect usage of the code may lead to baaaad corruption.

There is the unofficial _isPOD(_:) function you can use. As with all underscore APIs from swift this function might change or go away at any time.

1 Like

Oh super useful, thanks!

I'm working on debugging tool, so it will need constant validation against language and library versions anyway :)

1 Like

Note that "_isPOD" can return true for swift types. Those would be "ARC free" perhaps, but not necessarily "C-layout" compatible (should that matter).

2 Likes

I have briefly reviewed the Builtin.swift file. What an incredible source of power :supervillain: :smiley: I can now do all kinds of things. This really made me happy!

1 Like

Yes, thanks for pointing this out. I don't mind :slight_smile:

Edit: I think I can handle the layout issue with Mirror and MemoryLayout :slight_smile:

1 Like