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

Allow only 1-based indexed vectors in AbstractWeights #791

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 5 additions & 2 deletions src/weights.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ abstract type AbstractWeights{S<:Real, T<:Real, V<:AbstractVector{T}} <: Abstrac
@weights name

Generates a new generic weight type with specified `name`, which subtypes `AbstractWeights`
and stores the `values` (`V<:RealVector`) and `sum` (`S<:Real`).
and stores the `values` (`V<:RealVector` using 1-based indexing) and `sum` (`S<:Real`).
"""
macro weights(name)
return quote
mutable struct $name{S<:Real, T<:Real, V<:AbstractVector{T}} <: AbstractWeights{S, T, V}
values::V
sum::S
end
$(esc(name))(values::AbstractVector{<:Real}) = $(esc(name))(values, sum(values))
function $(esc(name))(values::AbstractVector{<:Real})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative that would be safer would be to define inner constructor:

macro weights(name)
    return quote
        mutable struct $(esc(name)){S<:Real, T<:Real, V<:AbstractVector{T}} <: AbstractWeights{S, T, V}
            values::V
            sum::S

            function $(esc(name)){S, T, V}(values::AbstractVector{T}, s::S) where {S<:Real, T<:Real, V<:AbstractVector{T}}
                Base.require_one_based_indexing(values)
                return new(values, s)
            end
        end

        $(esc(name))(values::AbstractVector{<:Real}, s=sum(values)) =
            $(esc(name)){typeof(s), eltype(values), typeof(values)}(values, s)
    end
end

but this looses all promotion rules that default inner constructor comes with.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we want to require 1-based indexing in all cases, the inner constructor approach sounds safer. How many methods do we need to define to replicate the default inner constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have closed the PR since it seems we want to support non-1-based indexing. I think this issue should be settled first.

Base.require_one_based_indexing(values)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return $(esc(name))(values, sum(values))
end
end
end

Expand Down