I can't access a lazy var on a struct, with an error about accessing a mutable getter:
struct TestModel: Decodable {
let longitude: Double
let latitude: Double
lazy var coordinate: CLLocationCoordinate2D = {
return CLLocationCoordinate2D(latitude: self.latitude, longitude: self.longitude)
}()
}
let jsonString = "{ \"longitude\": 45.0, \"latitude\": 70.0 }"
let data = jsonString.data(using: .utf8)!
let testModel = try? JSONDecoder().decode(TestModel.self, from: data)
print(testModel!.coordinate) // error: cannot use mutating getter on immutable value: 'testModel' is a 'let' constant
I understand that the first time accessing coordinate
, you've updated the storage associated with the struct, however from a programmer's prospective I don't believe the lazy var should be considered to mutate the struct. Is there something more to this or is this a good evolution candidate?