GeometryBasics.jl icon indicating copy to clipboard operation
GeometryBasics.jl copied to clipboard

ensure consistency among `Base.iterate` and `Base.in`

Open goretkin opened this issue 3 years ago • 1 comments

The definitions at

https://github.com/JuliaGeometry/GeometryBasics.jl/blob/234699e015ccb23bdaab26539dfc8fdfa9ff6add/src/basic_types.jl#L47-L49

are convenient, but they induce a non-geometric definition of Base.in, due to the fallback definition https://github.com/JuliaLang/julia/blob/4b739e711a7241f48ec0525e97c44cc870a9af99/base/operators.jl#L1109-L1120 and furthermore a geometric definition like the following

https://github.com/JuliaGeometry/GeometryTypes.jl/blob/4f1ba71d7df1341087f84974f869ea7a421eafa8/src/polygon.jl#L21-L28

are inconsistent with the documentation for Base.in. Since it is a shame to be inconsistent (because generic programs may break since they may rely on a consistency of iterate and in, and since it is a shame to lose the notation of point ∈ polygon, I propose ensuring that either

  1. a type represents an infinite set of points and does not implement iterate
  2. a type represents a finite set of points and implements iterate

goretkin avatar Nov 25 '20 21:11 goretkin

In case this example is helpful:

julia> Base.IteratorSize(::Type{<:GeometryBasics.Polygon}) = Base.SizeUnknown() # or define `length(::Polygon)` This is required by the [iterator interface](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-iteration)

julia> p = Polygon([Point(0,0), Point(0, 1), Point(1, 0)])
Polygon{2,Int64,Point{2,Int64},LineString{2,Int64,Point{2,Int64},Base.ReinterpretArray{GeometryBasics.Ngon{2,Int64,2,Point{2,Int64}},1,Tuple{Point{2,Int64},Point{2,Int64}},TupleView{Tuple{Point{2,Int64},Point{2,Int64}},2,1,Array{Point{2,Int64},1}}}},Array{LineString{2,Int64,Point{2,Int64},Base.ReinterpretArray{GeometryBasics.Ngon{2,Int64,2,Point{2,Int64}},1,Tuple{Point{2,Int64},Point{2,Int64}},TupleView{Tuple{Point{2,Int64},Point{2,Int64}},2,1,Array{Point{2,Int64},1}}}},1}}(GeometryBasics.Ngon{2,Int64,2,Point{2,Int64}}[Line([0, 0] => [0, 1]), Line([0, 1] => [1, 0])], LineString{2,Int64,Point{2,Int64},Base.ReinterpretArray{GeometryBasics.Ngon{2,Int64,2,Point{2,Int64}},1,Tuple{Point{2,Int64},Point{2,Int64}},TupleView{Tuple{Point{2,Int64},Point{2,Int64}},2,1,Array{Point{2,Int64},1}}}}[])

julia> Point(.2,.2) in p
false

julia> Point(0,0) in p
true

julia> (0,0) in p
false

julia> [0,0] in p
true

goretkin avatar Nov 25 '20 22:11 goretkin