Skip to content

Instantly share code, notes, and snippets.

@s1ddok
Created July 10, 2020 17:07
Show Gist options
  • Save s1ddok/77f5999c1046f3544f1021407317894d to your computer and use it in GitHub Desktop.
Save s1ddok/77f5999c1046f3544f1021407317894d to your computer and use it in GitHub Desktop.
class Checkpoint {
/// ...
var userInfo: [String: Codable] = [:]
}
protocol Checkpointable {
init(checkpoint: Checkpoint)
func read(from: Checkpoint)
func write(to: Checkpoint)
}
// make it optional
extension Checkpointable {
init(checkpoint: Checkpoint) { fatalError()}
}
struct Model: Checkpointable {
let hiddenLayerFeatureChannels: Int
init(hiddenLayerFeatureChannels: Int) {
self.hiddenLayerFeatureChannels = hiddenLayerFeatureChannels
}
init(checkpoint: Checkpoint) {
guard
let hiddenLayerFeatureChannels = checkpoint.userInfo["ngf"] as? Int
else { fatalError()}
self.init(hiddenLayerFeatureChannels: hiddenLayerFeatureChannels)
self.read(from: checkpoint)
}
func read(from: Checkpoint) {
// Normal reading
}
func write(to checkpoint: Checkpoint) {
// Normal writing
checkpoint.userInfo["ngf"] = hiddenLayerFeatureChannels
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment