Repository navigation
Install aftermarket expression functions - #11472
Conversation
|
f66b1ee converts |
|
Yes, they are complementary, but I don’t think they have to be coupled. We’re still going to need conventional functions, just in case the mechanism in this PR breaks for any reason. I’ve added items to the to-do list above for those PRs, in case I get to them first. |
e82177e to
15bf3d0
Compare
|
Thinking about this for a moment:
|
c989d11 to
8df1e98
Compare
|
0203d2d819f3c335c84e7db932953b30ad5f773f implements a catch-all |
There was a problem hiding this comment.
I suggested MGL_MATCH and MGL_SWITCH as counterparts to mgl_match: and mgl_case:, respectively, but I think I got mixed up. MGL_SWITCH implies the same semantics as MGL_MATCH. I do think “case” would be problematic, as it makes it impossible to refer to the cases inside the expression. “Ternary” is also problematic because there can be multiple cases. Do you think MGL_CONDITION or MGL_IF could work?
There was a problem hiding this comment.
I think MGL_IF will do the work. It's more descriptive to what the function actually does.
There was a problem hiding this comment.
This removes support for the conventional custom function mgl_match:. In this PR, I’ve left in support for those functions, just in case aftermarket expressions break with a new iOS SDK version. (I’ve equivocated about whether to remove public documentation about the conventional functions, but the implementations remain.) See the methods at the bottom of this file for an example of how to support both versions of the method simultaneously.
There was a problem hiding this comment.
These rows need to be updated.
There was a problem hiding this comment.
Rows need to be added for MGL_MATCH, MGL_SWITCH, and mgl_coalesce:.
There was a problem hiding this comment.
Right below here is an example that could potentially use MGL_MATCH. On the other hand, I do find the use of a dictionary with valueForKeyPath: to be somewhat elegant, so perhaps all it needs is a call to mgl_coalesce: to avoid the redundant valueForKeyPath: call.
Per chat with @fabian-guerra, we’re going to consider this defensive approach for a followup release, possibly on hold until we know there’s going to be a problem with an upcoming version of iOS or macOS. The existing code is already resilient to minor changes like the private class being renamed, just not to more substantial, public-facing changes to NSExpression. |
Only works for literal strings.
8874184 to
a51ae90
Compare
Renamed mgl_hasProperty:properties: to mgl_does:have: for better readability and consistency with the conventional mgl_has: function. Documented both forms of mgl_has:.
This is the preferred syntax for simple conditionals on iOS 9 and above, because you can inline the predicate instead of wrapping it in a constant value expression, which means you can write a conditional in a single format string.
| `at` | `objectFrom:withIndex:` | `array[n]` | ||
| `get` | `+[NSExpression expressionForKeyPath:]` | Key path | ||
| `has` | `mgl_hasProperty:properties:` | | ||
| `has` | `mgl_does:have:` | `mgl_does:have:(self, 'key')` |
There was a problem hiding this comment.
I renamed and reordered this function. Instead of calling mgl_hasProperty:properties:('key', self), you call mgl_does:have:(self, 'key'). I think this reads better: “Does self have ‘key’?” It’s also consistent with the order when you call the conventional function: FUNCTION(self, 'mgl_has:', 'key').
| `all` | `NSAndPredicateType` | `p0 AND … AND pn` | ||
| `any` | `NSOrPredicateType` | `p0 OR … OR pn` | ||
| `case` | `MGL_IF` | `MGL_IF(1 = 2, YES, 2 = 2, YES, NO)` | ||
| `case` | `+[NSExpression expressionForConditional:trueExpression:falseExpression:]` or `MGL_IF` | `TERNARY(1 = 2, YES, NO)` or `MGL_IF(1 = 2, YES, 2 = 2, YES, NO)` |
There was a problem hiding this comment.
I restored full support for +[NSExpression expressionForConditional:trueExpression:falseExpression:] where available, along with the tests for it. On iOS 9.0 and above, we convert case to TERNARY() when it has only one conditional. This is the preferred syntax, because you can inline the predicate instead of wrapping it in a constant value expression, which means you can write a conditional in a single format string.
/cc @friedbunny
|
The code ticks in the table at |
Install Mapbox-specific functions as first-class NSExpression functions, prefixed to avoid collisions with any additional functions that may be built into NSExpression in the future. Various functions need to be renamed because the aftermarket variant no longer takes an explicit operand.
The result of this PR is that nontrivial expression usage will be a lot more legible and easier to write by hand without frequently consulting a reference. For example, concatenating strings goes from:
to:
boolValuemgl_expressionWithContext:→MGL_LET()mgl_interpolateWithCurveType:parameters:stops:→mgl_interpolate:withCurveType:parameters:stops:mgl_numberWithFallbackValues:,doubleValue,floatValue,decimalValue→CAST()mgl_stepWithMinimum:stops:→mgl_step:from:stops:stringByAppendingString:→mgl_join:stringValue→CAST()mgl_matchWithOptions:default:→MGL_MATCH(followup to [ios, macos] Add match expressions support. #11464)mgl_coalesce→mgl_coalesce:(followup to [ios, macos] Add match expressions support. #11464)mgl_case→MGL_IF()(followup to [ios, macos] Change the format for case expressions to a flat structure. #11450)mgl_has:→mgl_does:have:(followup to [ios, macos] Add lookup and feature operators. #11528)MGL_FUNCTION()to unblock Eviscerate mbgl expression to Foundation JSON object conversion #11389Safeguard against changes to NSExpression that render this approach ineffectiveRemove redundantFUNCTION()-based functions?Fixes #11015.
/cc @fabian-guerra @jmkiley @akitchen