JuliaGraphics / Luxor.jl

Simple drawings using vector graphics; Cairo "for tourists!"
http://juliagraphics.github.io/Luxor.jl/
Other
588 stars 71 forks source link

Remove methods like `+(::Number, ::Point)` and update around broadcasting #294

Closed hyrodium closed 7 months ago

hyrodium commented 7 months ago

This PR fixes https://github.com/JuliaGraphics/Luxor.jl/issues/270.

Before this PR

julia> using Luxor

julia> Point(1,2) .+ 3
Point(4.0, 5.0)

julia> Point(1,2) + 3  # Should throw an error like `(1,2) + 3` or `[1,2] + 3`
Point(4.0, 5.0)

julia> [Point(1,2), Point(4,3)] .+ Ref(Point(0,-2))
2-element Vector{Point}:
 Point(1.0, 0.0)
 Point(4.0, 1.0)

julia> [Point(1,2), Point(4,3)] .+ Point(0,-2)
2-element Vector{Point}:
 Point(1.0, 0.0)
 Point(4.0, 1.0)

julia> Point(1,2) + (3,4)
Point(4.0, 6.0)

julia> Point(1,2) .+ (3,4)  # Should be equal to `Point(1,2) + (3,4)`
(Point(4.0, 5.0), Point(5.0, 6.0))

julia> Point(1,2) .+ [3,4]  # Should be 2-element collection
2-element Vector{Point}:
 Point(4.0, 5.0)
 Point(5.0, 6.0)

After this PR

julia> using Luxor

julia> Point(1,2) .+ 3
Point(4.0, 5.0)

julia> Point(1,2) + 3
ERROR: MethodError: no method matching +(::Point, ::Int64)
[...]

julia> [Point(1,2), Point(4,3)] .+ Ref(Point(0,-2))  # Now requires `Ref` for broadcasting
2-element Vector{Point}:
 Point(1.0, 0.0)
 Point(4.0, 1.0)

julia> [Point(1,2), Point(4,3)] .+ Point(0,-2)
ERROR: MethodError: no method matching +(::Point, ::Float64)
[...]

julia> Point(1,2) + (3,4)
Point(4.0, 6.0)

julia> Point(1,2) .+ (3,4)
Point(4.0, 6.0)

julia> Point(1,2) .+ [3,4]
2-element Vector{Float64}:
 4.0
 6.0
codecov[bot] commented 7 months ago

Codecov Report

Attention: 7 lines in your changes are missing coverage. Please review.

Comparison is base (87ae952) 73.96% compared to head (8507466) 73.94%.

Files Patch % Lines
src/point.jl 63.15% 7 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #294 +/- ## ========================================== - Coverage 73.96% 73.94% -0.02% ========================================== Files 36 36 Lines 6680 6687 +7 ========================================== + Hits 4941 4945 +4 - Misses 1739 1742 +3 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

cormullion commented 7 months ago

Cool, thanks! This looks like a version 4.0... :)

hyrodium commented 7 months ago

Should we create a branch release/v3.9 to release v3.9.0 with deprecation messages? If so, I will create a PR for that!

cormullion commented 7 months ago

Should we create a branch release/v3.9 to release v3.9.0 with deprecation messages? If so, I will create a PR for that!

Cool, thanks! Can you do this using the commit I've just uploaded? - I've been fixing the broadcasting regressions in the docs...

    # -> 3.9
    # rule.(between.(O - (250, 0), O + (250, 0), 0:0.01:1), -π/3)
    # 4.0 ->
    rule.(between.(Ref(O - (250, 0)), Ref(O + (250, 0)), 0:0.01:1), -π/3)

I write the 3.9 form all the time... :)