Repository navigation
Add convenience initializers to shape source - #7665
Conversation
bb0446f to
6b99f55
Compare
|
Do we actually need a |
There was a problem hiding this comment.
Should we check the type of each object in features and log a one time warning if a non-feature makes it through?
There was a problem hiding this comment.
Since lightweight generic qualifiers are completely optional in Objective-C, I think it’s fairly likely that a developer would incorrectly attempt to pass in an array of MGLShapes, especially if we get rid of -initWithIdentifier:shapes:options:. I think we should raise an exception in that case, since the value would contradict the type expected by this method.
1ec5
left a comment
There was a problem hiding this comment.
In addition to the comments below, refer to -initWithIdentifier:features:options: in the -initWithIdentifier:shape:options: initializer as a way to put multiple features in the same source.
There was a problem hiding this comment.
This method takes an array of features, not a single shape collection feature object.
There was a problem hiding this comment.
It does take an array but that is not what it returns in the "shape" property of the instantiated object. Isn't that what this sentence is trying to explain?
There was a problem hiding this comment.
This sentence explains what you pass into the method.
There was a problem hiding this comment.
Ah of course! I think not seeing "initialized with" threw me off for some reason today :D
There was a problem hiding this comment.
Maybe this could be a @note instead? My thinking is that we should take every chance we have to point out the subtle difference between shapes and features. If we do get rid of -initWithIdentifier:shapes:options: then this note could probably go away completely.
There was a problem hiding this comment.
Unlike
-initWithIdentifier:shapes:options:, this method acceptsMGLFeatureinstances, such asMGLPointFeatureobjects, whose attributes you can use when applying a predicate toMGLVectorStyleLayeror configuring a style layer’s appearance.
There was a problem hiding this comment.
This method takes an array of shapes, not a single shape collection object.
There was a problem hiding this comment.
An array of objects that conform to the
MGLFeatureprotocol.
There was a problem hiding this comment.
Missing a period, and “subclass instances” is a bit confusing:
An array of shapes; each shape is a member of a concrete subclass of
MGLShape.
There was a problem hiding this comment.
NS_ARRAY_OF(MGLFeature) * resolves to NSArray<MGLFeature> *, meaning the passed-in value must be an NSArray object that conforms to the MGLFeature protocol. I think what you meant was NS_ARRAY_OF(id <MGLFeature>) *.
There was a problem hiding this comment.
Correct. The header is written that way but it did not make it here somehow.
There was a problem hiding this comment.
Since lightweight generic qualifiers are completely optional in Objective-C, I think it’s fairly likely that a developer would incorrectly attempt to pass in an array of MGLShapes, especially if we get rid of -initWithIdentifier:shapes:options:. I think we should raise an exception in that case, since the value would contradict the type expected by this method.
|
Also, can you see if the “Working with GeoJSON Data” guide needs any updates? |
There was a problem hiding this comment.
Any
MGLFeatureinstance passed into this initializer is treated as an ordinary shape, causing any attributes to be inaccessible to anMGLVectorStyleLayerwhen evaluating a predicate or configuring certain layout or paint attributes. To preserve the attributes associated with each feature, use the-initWithIdentifier:features:options:or-initWithIdentifier:shape:options:method instead.
There was a problem hiding this comment.
The documentation for this method currently says:
To include multiple shapes in the source, use an
MGLShapeCollectionorMGLShapeCollectionFeatureobject.
Change it to:
To include multiple shapes in the source, use an
MGLShapeCollectionorMGLShapeCollectionFeatureobject, or use the-initWithIdentifier:features:options:or-initWithIdentifierr:shapes:options:method.
3a6f2f4 to
6ba0034
Compare
|
Updated the guide in 6ba0034 |
There was a problem hiding this comment.
method to create a shape source with an array
There was a problem hiding this comment.
You likely had to cast here because +[MGLShapeCollectionFeature shapeCollectionWithShapes:] accepts an array of MGLShape <MGLFeature> * instead of an array of id <MGLFeature>. Let’s make this method and +[MGLShapeCollectionFeature shapeCollectionWithShapes:] consistent with each other. (I have no strong preference one way or the other, but id <MGLFeature> might leave more options open for fixing #7454.)
There was a problem hiding this comment.
Since MGLShapeCollectionFeature inherits from MGLShapeCollection and MGLShapeCollection uses an NS_ARRAY_OF(MGLShape *) I don't think I can change MGLShapeCollectionFeature since it would require the change to bubble up to MGLShapeCollection 😬
The implementation of MGLShapeCollectionFeature does not use lightweight generics (just a plain old NSArray). I made the same change to MGLShapeSource in 6287c6b. Are you ok with this solution for this case, for now?
There was a problem hiding this comment.
The implementation of +[MGLShapeCollectionFeature shapeCollectionWithShapes:] uses an unqualified NSArray in its signature, but the declaration has a qualified NSArray<MGLShape<MGLFeature> *> *. Omitting the qualifier in the implementation only takes away opportunities for the compiler to catch mistakes. We should qualify as much as possible.
Since +[MGLShapeCollectionFeature shapeCollectionWithShapes:] is declared to take an NSArray<MGLShape<MGLFeature> *> * rather than the broader type NSArray<id<MGLFeature>> *, -[MGLShapeSource initWithIdentifier:features:options:] should take an NSArray<MGLShape<MGLFeature> *> * as well.
There was a problem hiding this comment.
Got it. This is done in e85da7f. I'll merge after the build passes.
This adds two new convenience initializers to MGLShapeSource: -initWithIdentifier:features:options: takes an array of shape objects that conform to MGLFeature, inserts them in a shape collection feature and creates a source with that shape. -initWithIdentifier:shapes:options does the same but with concrete MGLShape objects that get added to a shape collection. Throw an exception if an shape source is created with the features initializer but is sent an array of features that contains something that is not actually an object that conforms to the feature protocol. Updates to geojson data guide Qualify APIs that take arrays of shapes that are features
e85da7f to
4166635
Compare
Fixes #7453
This adds two new convenience initializers to MGLShapeSource:
-initWithIdentifier:features:options:takes an array of shape objects that conform to MGLFeature, inserts them in a shape collection feature, and creates a source with that shape.-initWithIdentifier:shapes:options:does the same but with concrete MGLShape objects that get added to a shape collection.Although both are technically convenience initializers, developers will most often want to use -
initWithIdentifier:features:options:since features objects unlock key runtime styling functionality. So, the inline example has been updated to demonstrateMGLShapeSourceusage with-initWithIdentifier:features:options:The type system will help ensure that Swift developers use the correct convenience initializer for objects that are features (i.e. it is not possible to send plain shapes to the features initializer without an unsafe cast). There is currently no warning system for this though so Objective-C developers will not be protected from bugs where layer properties or predicate filters don't work as expected because plain shapes were previously used with the features initializer.
cc @frederoni @bsudekum @ericrwolfe