Dynamic Member Lookup introduced in Swift 4.2, now we can use dot, instead of brackets - #986
Conversation
…that use it provide "dot" syntax for arbitrary names which are resolved at runtime - in a completely type safe way. This provides syntactic sugar that allows the user to write **[introduce-user-defined-dynamic-member-lookup-types]**(https://github.com/apple/swift-evolution/blob/master/proposals/0195-dynamic-member-lookup.md#introduce-user-defined-dynamic-member-lookup-types) i've added a subscript function with dynamic member, so users can use dot instead of [], ``` let name = json.name The same as: let name = json["name"] let phoneNumber = json.user.contactInfo.phoneNumber The same as: let name = json["user"]["contactInfo"]["phoneNumber"] ```
| XCTAssertEqual(json["user", "feeds"], [98833, 23443, 213239, 23232]) | ||
| } | ||
| func testMultilevelDynamicLookUpSetter() { | ||
| var json: JSON = ["user": ["id": 987654, "info": ["name": "farshad", "email": "farshadjahanmanesh@gmail.com"], "feeds": [98833, 23443, 213239, 23232]]] |
There was a problem hiding this comment.
Line Length Violation: Line should be 120 characters or less: currently 159 characters (line_length)
| XCTAssertTrue(json.z == "zoo") | ||
| } | ||
| func testMultilevelDynamicLookUpGetter() { | ||
| var json: JSON = ["user": ["id": 987654, "info": ["name": "farshad", "email": "farshadjahanmanesh@gmail.com"], "feeds": [98833, 23443, 213239, 23232]]] |
There was a problem hiding this comment.
Line Length Violation: Line should be 120 characters or less: currently 159 characters (line_length)
| XCTAssertTrue(json.a == "aoo") | ||
| XCTAssertEqual(json.bb, JSON("bpp")) | ||
| XCTAssertTrue(json.z == "zoo") | ||
|
|
There was a problem hiding this comment.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
|
unknown attribute 'dynamicMemberLookup' Types that use it provide "dot" syntax for arbitrary names which are resolved at runtime - in a completely type safe way. am i doing something wrong? |
|
I will investigate this awesome evolution in a few days. |
|
retest with new travis image |
|
I would prefer to use another swift file to hold these kinds of test cases -- Don't forget to add the as-is LICENSE header to the new file. |
| } | ||
| func testMultilevelDynamicLookUpSetter() { | ||
| var json: JSON = ["user": ["id": 987654, "info": | ||
| ["name": "farshad","email": "farshadjahanmanesh@gmail.com"], |
There was a problem hiding this comment.
Comma Spacing Violation: There should be no space before and one after any comma. (comma)
agree, i have created a new test file for dynamic member lookup |
|
Why is this feature not merged into the main branch |
This proposal introduces a new @dynamicMemberLookup attribute. Types that use it provide "dot" syntax for arbitrary names which are resolved at runtime - in a completely type safe way. This provides syntactic sugar that allows the user to write
introduce-user-defined-dynamic-member-lookup-types
i've added a subscript function with dynamic member, so users can use dot instead of [],