Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// a.swift
public dynamic func f() {
print("original")
}
// b.swift
import a
@_dynamicReplacement(for: f())
public func f_b() {
print("replacement in b")
}
// c.swift
import Foundation
import a
f()
let file = URL(fileURLWithPath: "/path/to/libb.dylib")
guard let handle = dlopen(file.path, RTLD_NOW) else {
fatalError("dlopen failed")
}
f()
/*
$ swiftc -emit-module -module-name a a.swift
$ swiftc -emit-library -module-name a a.swift
$ swiftc -emit-library -module-name b -I. liba.dylib b.swift
$ swiftc -emit-executable -module-name c -I . liba.dylib c.swift
$ ./c
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment