Skip to content

conversion from OffsetArray to Array #239

@johnnychen94

Description

@johnnychen94

This is probably not a well-defined use case because we do not know if the users really want the offsets information being dropped:

x = [1, 2, 3, 4]
xo = OffsetArray(x, -1)
convert(typeof(x), xo) # DimensionMismatch

Currently, we use OffsetArrays.no_offset_view to do such work:

OffsetArrays.no_offset_view(xo) === x # true

Do you think we need to 1) force a non-offset conversion:

Base.convert(::Type{AT}, M::OffsetArray) = convert(AT, no_offset_view(M))

or 2) just throw an error (the status quo) but with a friendly error message using register_error_hint? An example can be found in ColorVectorSpace:

julia> using ImageCore

julia> x = rand(RGB)
RGB{Float64}(0.04112895224616153,0.13887365079062053,0.6095527179104803)

julia> abs2(x)
ERROR: MethodError: no method matching abs2(::RGB{Float64})
It is ambiguous whether `abs2(::RGB{Float64})` should return a real number or a color object.
You can use `_abs2(c) = mapreducec(v->float(v)^2, +, 0, c)` to get the value compatible with ColorVectorSpace v0.8 or earlier.
Closest candidates are:
  abs2(::Bool) at bool.jl:80
  abs2(::Real) at number.jl:151
  abs2(::Complex) at complex.jl:265
  ...
Stacktrace:
 [1] top-level scope
   @ REPL[5]:1

I prefer option 2 here, i.e., throw an error with an appropriate hint.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions