Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure consistency among Base.iterate and Base.in #107

Open
goretkin opened this issue Nov 25, 2020 · 1 comment
Open

ensure consistency among Base.iterate and Base.in #107

goretkin opened this issue Nov 25, 2020 · 1 comment

Comments

@goretkin
Copy link

The definitions at

@propagate_inbounds Base.getindex(x::Polytope, i::Integer) = coordinates(x)[i]
@propagate_inbounds Base.iterate(x::Polytope) = iterate(coordinates(x))
@propagate_inbounds Base.iterate(x::Polytope, i) = iterate(coordinates(x), i)

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant