I have this struct:
struct Foo {
var name: String = "hello"
var count: Int = 42
}
When I add @dynamicMemberLookup
on it compiler rightfully complains:
So, I've added the relevant subscript implementation:
@dynamicMemberLookup
struct Foo {
var name: String = "hello"
var count: Int = 42
subscript<T>(dynamicMember keyPath: WritableKeyPath<Foo, T>) -> T {
get {
fatalError("TODO")
}
set {
fatalError("TODO")
}
}
}
and now compiler is happy.
However, it has no effect:
var foo = Foo()
foo.name = "bye"
print(foo.name)
and my subscript getter/setters are not getting called †.
Is this a bug? if this is a bug then what bug is this exactly:
- that it doesn't work (as "expected"), and it should.
- that compiler does accept this subscript as a valid dynamicMember implementation and it shouldn't.
(† I know how to make it work with a slightly different keypath subscript and a separate helper type, as discussed e.g. here).