JuliaLang/julia

unary `+` acts differently for `Array` and `AbstractArray`

開放

#58,295 建立於 2025年5月1日

 (7 則留言) (2 個反應) (0 位負責人)Julia (5,773 個分叉)batch import
arraysdesigngood first issue

倉庫指標

星標
 (48,709 顆星)
PR 合併指標
 (平均合併 20天 6小時) (30 天內合併 157 個 PR)

描述

(This issue is tangentially related to #58281.)

The return type of unary + with Bool argument is not the same for arrays and views:

julia> v = [true, false]; +v
2-element Vector{Int64}:
 1
 0

julia> w = view(v, :); +w
2-element view(::Vector{Bool}, :) with eltype Bool:
 1
 0

The reason is that unary + for AbstractArray is simply the identity map:

https://github.com/JuliaLang/julia/blob/748775b5689deaa4acac7929e0a31a80d1c564f4/base/abstractarraymath.jl#L285

Hence it misses the convention that + transforms Bool to Int.

Another consequence of this definition is that one doesn't know whether +v is identical to v for mutable v:

julia> v = [1, 2]; +v === v
false

julia> w = view(v, :); +w === w
true

In my opinion, it would be good to have a consistent answer. However, it may be to late for that.

貢獻者指南