Skip to content

Stop encouraging type-piracy; suggest "dynamic" vendoring #56

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ spanning from `a` to `b`, all values `x` that lie between `a` and `b`
are defined as being members of the interval.

This package is intended to implement a "minimal" foundation for
intervals upon which other packages might build. In particular, we
*encourage* [type-piracy](https://docs.julialang.org/en/stable/manual/style-guide/#Avoid-type-piracy-1)
for the reason that only one interval package can
unambiguously define the `..` and `±` operators (see below).
intervals upon which other packages might build. If you find
IntervalSets.jl lacks definitions you need, we suggest to directly
`include` IntervalSets.jl in your package rather than the standard
`using` or `import` (see below) to avoid [type-piracy](https://docs.julialang.org/en/stable/manual/style-guide/#Avoid-type-piracy-1).

Currently this package defines one concrete type, `ClosedInterval`.
These define the closed set spanning from `a` to `b`, meaning the
Expand Down Expand Up @@ -84,3 +84,27 @@ julia> (0.25..5) ∪ (6..7.4)

ArgumentError: Cannot construct union of disjoint sets.
```

## Including IntervalSets.jl as a submodule

To avoid multiple packages overwriting method definitions, you can
`include` IntervalSets.jl instead of the standard `using` or `import`:

```julia
module YourPackage

include(Base.locate_package(Base.PkgId(Base.UUID("8197267c-284f-5f27-9208-e0e47529a953"), "IntervalSets")))
using .IntervalSets # notice the leading dot

# ... more code ...

end
```

This defines `IntervalSets` to be a _submodule_ of `YourPackage`. It
means that all functions and types in `YourPackage.IntervalSets` are
"owned" by `YourPackage` hence any method can be implemented without
worrying about type-piracy. Note that re-exporting symbols from
`YourPackage.IntervalSets` such as `..` and `±` is not a good idea
because `YourPackage.IntervalSets.±` and `IntervalSets.±` are
different functions.
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -693,4 +693,10 @@ struct IncompleteInterval <: AbstractInterval{Int} end
@test_throws ErrorException endpoints(I)
@test_throws ErrorException closedendpoints(I)
end

@testset "vendoring" begin
m = Module()
Base.include(m, Base.locate_package(Base.PkgId(Base.UUID("8197267c-284f-5f27-9208-e0e47529a953"), "IntervalSets",)))
@test m.IntervalSets !== IntervalSets
end
end