Skip to content

Commit 88da506

Browse files
authored
Add a convert method for (module, trait, geom) (#194)
This allows the trait to be passed through completely from the time of discovery to the bottom level, which decreases allocations and dynamic dispatch in long call chains like GeometryOps' `apply`.
1 parent e369f0c commit 88da506

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/fallbacks.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ calc_extent(t::AbstractFeatureCollectionTrait, fc) = reduce(Extents.union, filte
148148
# corresponding geometry type
149149
function convert(package::Module, geom)
150150
t = trait(geom)
151+
convert(package, t, geom)
152+
end
153+
function convert(package::Module, t::AbstractTrait, geom)
151154
isdefined(package, :geointerface_geomtype) || throw(ArgumentError("$package does not implement `geointerface_geomtype`. Please request this be implemented in a github issue."))
152155
convert(package.geointerface_geomtype(t), t, geom)
153156
end

src/interface.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,21 @@ geom type for the trait using the modules `geointerface_traittype` method.
673673
674674
`convert(T::Type)` or `convert(m::Module)` return curried versions of that function,
675675
just like `==`.
676+
677+
# Extended help
678+
679+
For developers, the base implementation is `GeoInterface.convert(::YourGeomType, ::AbstractTrait, some_other_geom)`.
680+
This is the method signature that must be implemented for a custom geometry type.
681+
682+
Users may also call this method directly, if they know the exact output - this can sometimes
683+
save the Julia compiler some work and cause more optimal code to be generated.
684+
685+
For convenience and type stability there is a `convert(::Module, ::AbstractTrait, geom)` method
686+
that is particularly useful when the trait of a geometry needs to be propagated deep within the
687+
stack.
688+
689+
This helps to prevent the compiler from losing the trait information, especially useful
690+
when dealing with sumtype-like implementations or opaque pointers from C libraries.
676691
"""
677692
convert(T, geom) = convert(T, geomtrait(geom), geom)
678693
convert(::Type{T}, x::T) where {T} = x # no-op

0 commit comments

Comments
 (0)