Skip to content

Instantly share code, notes, and snippets.

@alemar11
Forked from omochi/dynamicReplacement.swift
Created April 4, 2020 15:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alemar11/d2ed3a90dd437267b156f8f33996e8af to your computer and use it in GitHub Desktop.
Save alemar11/d2ed3a90dd437267b156f8f33996e8af to your computer and use it in GitHub Desktop.
// 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