mapbox/turf-swift

Geometric types should conform to common protocol that converts to Geometry

1ec5 opened this issue · 3 comments

1ec5 commented

GEOSwift defines a GeometryConvertible protocol that makes it possible to initialize a Feature without redundantly specifying both the Geometry case and the geometric type’s initializer at the same time. Turf’s GeoJSONObject and Geometry enumerations should have something similar. For example:

var geoJSON = Feature(geometry: .point(Point(coordinate)))

would become simply:

var geoJSON = Feature(geometry: Point(coordinate))

It’s similar to the initializers and literals that #154 added to JSONValue, but this protocol distributes the implementation across each type:

/// Initializes a JSON value representing the given Boolean value.
public init(_ bool: Bool) {
self = .boolean(bool)
}

/cc @macdrevx

1ec5 commented

A similar JSONValueConvertible protocol might eliminate a major backwards-incompatible change that arose in #154, making both syntaxes equivalent again:

feature.properties = [
    "literal": "Jason",
    "non-literal": .string(name),
]
feature.properties = [
    "literal": "Jason",
    "non-literal": name,
]
1ec5 commented

A similar JSONValueConvertible protocol might eliminate a major backwards-incompatible change that arose in #154, making both syntaxes equivalent again

I’ve been trying various gymnastics but haven’t managed to make this work without making [String: Any?] conform to ExpressibleByArrayLiteral, which would be overbroad.

1ec5 commented

A similar JSONValueConvertible protocol might eliminate a major backwards-incompatible change that arose in #154, making both syntaxes equivalent again

Now tracking this feature separately in #163.