Skip to content

Commit bf03dca

Browse files
committed
clean up and document register macro
1 parent 97f292d commit bf03dca

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

src/Polynomials.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ include("polynomials/Poly.jl") # Deprecated -> Will be removed
1717
include("pade.jl")
1818
include("deprecated.jl")
1919

20-
2120
# Interface for all AbstractPolynomials
2221
include("common.jl")
2322

src/abstract.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ An abstract container for various polynomials.
1414
abstract type AbstractPolynomial{T<:Number} end
1515

1616

17+
"""
18+
Polynomials.@register(name)
19+
20+
Given a polynomial with `name`, creates some common convenience constructors and conversions to minimize code required for implementation of a new polynomial type.
21+
22+
# Example
23+
```julia
24+
struct MyPolynomial{T} <: AbstractPolynomial{T} end
25+
26+
Polynomials.@register MyPolynomial
27+
```
28+
29+
# Implementations
30+
This will implement simple self-conversions like `convert(::Type{MyPoly}, p::MyPoly) = p` and creates two promote rules. The first allows promotion between two types (e.g. `promote(Polynomial, ChebyshevT)`) and the second allows promotion between parametrized types (e.g. `promote(Polynomial{T}, Polynomial{S})`).
31+
32+
For constructors, it implements the shortcut for `MyPoly(...) = MyPoly{T}(...)`, singleton constructor `MyPoly(x::Number, ...)`, and conversion constructor `MyPoly{T}(n::S, ...)`.
33+
"""
1734
macro register(name)
1835
poly = esc(name)
1936
quote
@@ -37,6 +54,5 @@ macro register(name)
3754
$poly(n::Number, var = :x) = $poly([n], var)
3855
$poly{T}(n::S, var = :x) where {T,S<:Number} = $poly(T(n), var)
3956
$poly{T}(x::AbstractVector{S}, var = :x) where {T,S<:Number} = $poly(T.(x), var)
40-
4157
end
4258
end

src/polynomials/Poly.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ of `Polynomial`
55

66
export Poly
77

8-
struct Poly{T} <: AbstractPolynomial{T}
8+
struct Poly{T<:Number} <: AbstractPolynomial{T}
99
coeffs::Vector{T}
1010
var::Symbol
1111
function Poly(a::AbstractVector{T}, var::SymbolLike = :x) where {T <: Number}

0 commit comments

Comments
 (0)