I'm trying to write a macro that will replace a stored property with a computed property similar to how the Observable
macro works. But I can't get this to work for properties that define initial values. I have something like this:
extension Foo {
@Bar static let someFoo = Foo()
}
Which I want to translate into something like this:
extension Foo {
static var someFoo: Foo {
// some extra logic here
return _someFoo
}
private static var _someFoo = Foo()
}
However, it seems like the annotated property can only be modified in the specific ways that the macro system allows. So I can add the private stored property, and I can add an accessor to the original property, but the property remains as it was written, so I end up with this:
extension Foo {
static let someFoo = Foo() {
// some extra logic here
return _someFoo
}
private static var _someFoo = Foo()
}
Which is invalid because the computed property is a let
and has an initial value.
I could require writing var
instead (though I'd prefer to keep using let
), but I'd still need to somehow replace the initial value with a type annotation.
Am I overlooking something? How does the Observable
macro accomplish this?