SwiftQuiz
#17

@dynamicMemberLookup Over a Private Field

  • dynamic-member-lookup
Swift 6.3.2 Report an issue

Verified quizzes have their expected answer checked by compiling and running the code with the listed Swift toolchain during the site build.

@dynamicMemberLookup
struct Wrap {
    var x = 0
    private var y = 7
    subscript(dynamicMember _: String) -> Int {
        99
    }
}

print(Wrap().y)
Hint

y exists on Wrap. From outside the type, can the compiler see it? If it can't, what's the next thing it tries?

Your answer

What does this program do?