Others have discussed how to call C code from Swift, and it works nicely. Others have also discussed how calling Swift as a subroutine to C code is a bad idea, because the whole Swift runtime would need to be set up.
But here's my question: if my program is based in Swift, and calls C subroutines, but would like to provide callbacks for those subroutines, is that possible? And could those C subroutines call Swift routines by name, provided that they took C compatible typed parameters (CInt, etc)?
Also, can C and Swift share global variables? In either direction?
The approved way to do this kind of thing is assigning swift functions/closures to C function pointers.
But if you look at the Swift source code, it uses the undocumented
@_silgen_name
attribute in several places to give swift functions C compatible names, so they can be called directly from C and C++So this works (tested in XCode 9 beta)
main.c
SomeSwift.swift
But given it's undocumented you probably don't want to use it, and it's a bit murky on what function signatures and parameter types it will work with. ABI stability anyone??