Skip to content

Instantly share code, notes, and snippets.

@omidgolparvar
Created September 13, 2025 10:11
Show Gist options
  • Select an option

  • Save omidgolparvar/018a76344c73375755463c16c384b79b to your computer and use it in GitHub Desktop.

Select an option

Save omidgolparvar/018a76344c73375755463c16c384b79b to your computer and use it in GitHub Desktop.
/// https://www.swiftbysundell.com/articles/let-vs-var-for-swift-struct-properties/
@propertyWrapper struct Readonly<Value> {
let wrappedValue: Value
}
extension Readonly: Encodable where Value: Encodable {
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(wrappedValue)
}
}
extension Readonly: Decodable where Value: Decodable {
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
wrappedValue = try container.decode(Value.self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment