# What is Base.maybeview?

**URL:** <https://discourse.julialang.org/t/what-is-base-maybeview/58820>\
**Category:** General Usage\
**Created:** [April 8, 2021, 11:17am UTC](https://discourse.julialang.org/t/what-is-base-maybeview/58820 "2021-04-08T11:17:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [April 8, 2021, 11:17am UTC](https://discourse.julialang.org/t/what-is-base-maybeview/58820/1 "2021-04-08T11:17:16Z")

</div>

I found usage of `Base.maybeview` in `LSHFunctions`. What is it? (no docs). How is it different from `view`? thanks.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 8, 2021, 11:23am UTC](https://discourse.julialang.org/t/what-is-base-maybeview/58820/2 "2021-04-08T11:23:32Z")

</div>

It is documented [in the source](https://github.com/JuliaLang/julia/blob/027bd4a4d17979e5f78ca6831cac456dc41d1db8/base/views.jl#L143):

> maybeview is like getindex, but returns a view for slicing operations  
> (while remaining equivalent to getindex for scalar indices and non-array types)

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [April 8, 2021, 11:28am UTC](https://discourse.julialang.org/t/what-is-base-maybeview/58820/3 "2021-04-08T11:28:10Z")

</div>

yes I saw that piece, but not understand …

could an example be given? (that could not be replaced by `view`). thanks.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 8, 2021, 11:41am UTC](https://discourse.julialang.org/t/what-is-base-maybeview/58820/4 "2021-04-08T11:41:40Z")

</div>

It handles scalar indexes and non-array types.

Incidentally, it is an _internal_ function in rather old code. You should not be using it outside `Base`. If you are making a PR to this code and want to remove/extend it, ask in the code review.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [April 8, 2021, 12:54pm UTC](https://discourse.julialang.org/t/what-is-base-maybeview/58820/5 "2021-04-08T12:54:30Z")

</div>

the problem is it’s used inside `LSHFunctions` where I found that usage could be replaced by `view`…

and I’m trying to make an in-place version of `LSHFunctions` so wanna to understand every line of code there…

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 8, 2021, 1:01pm UTC](https://discourse.julialang.org/t/what-is-base-maybeview/58820/6 "2021-04-08T13:01:14Z")

</div>

> [@tomtom](#):
>
> `LSHFunctions`

I imagine you would get more specific answers if you asked the maintainers of that package.

That said, I can’t find `maybeview` at all in that repository.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [April 8, 2021, 1:12pm UTC](https://discourse.julialang.org/t/what-is-base-maybeview/58820/7 "2021-04-08T13:12:27Z")

</div>

It’s the difference between:

```nohighlight
julia> x = rand(1,3)
1×3 Matrix{Float64}:
 0.767098 0.88126 0.19787

julia> view(x, 1, 1)
0-dimensional view(::Matrix{Float64}, 1, 1) with eltype Float64:
0.7670979357324583

julia> @views x[1,1]
0.7670979357324583

```

The latter is internally doing `Base.maybeview` and returning a scalar value directly instead of a 0-dimensional array.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [April 8, 2021, 1:36pm UTC](https://discourse.julialang.org/t/what-is-base-maybeview/58820/8 "2021-04-08T13:36:49Z")

</div>

```julia
julia> using LSHFunctions

julia> v = randn(3);

julia> hashfun = L2Hash(5);

julia> hashfun(v)
5-element Array{Int32,1}:
 -1
  9
  2
 -3
 -6

julia> @enter hashfun(v)
In LpHash(x) at /Users/Thomas/.julia/packages/LSHFunctions/A9iRT/src/hashes/lphash.jl:221
  4 │ %4 = (LSHFunctions.current_max_input_size)(hashfn)
  5 │ %5 = (>)(%3, %4)
  6 └── goto #3 if not %5
  7 2 ─ (resize!)(hashfn, n)
  8 3 ┄ S#274 = (getproperty)(hashfn, :coeff)
  9 │ %9 = S#274
 10 │ %10 = (lastindex)(S#274, 1)
 11 │ %11 = (Colon())(1, %10)
 12 │ %12 = (Colon())(1, n)
 >13 │ %13 = (Base.maybeview)(%9, %11, %12)
 14 │ h = (*)(%13, x)
 15 │ %15 = h

```

in the above, `%9` is a float32 concrete matrix `coeff`; `%13` is the `maybeview` of this matrix, which, **I don’t understand why a `maybeview` (or even a `view`) is needed for the subsequent matrix multiplication** `h = (*)(%13, x)`…

#####################  
😃problem solved by @mbauman above: use of `maybeview` here is to enable the switch to scalar multiplication (from matrix multiplication) when `x` has length 1.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [April 8, 2021, 1:40pm UTC](https://discourse.julialang.org/t/what-is-base-maybeview/58820/9 "2021-04-08T13:40:16Z")

</div>

thanks @mbauman !

```julia
julia> aa = randn(3, 2)
3×2 Array{Float64,2}:
 1.02029 -0.0648397
 0.401972 1.44311
 1.0134 0.881608

julia> view(aa, 1, 1)
0-dimensional view(::Array{Float64,2}, 1, 1) with eltype Float64:
1.0202872251901545

julia> Base.maybeview(aa, 1, 1)
1.0202872251901545

julia> view(aa, 1, 1:2)
2-element view(::Array{Float64,2}, 1, 1:2) with eltype Float64:
  1.0202872251901545
 -0.06483974101638355

julia> Base.maybeview(aa, 1, 1:2)
2-element view(::Array{Float64,2}, 1, 1:2) with eltype Float64:
  1.0202872251901545
 -0.06483974101638355

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 8, 2021, 2:23pm UTC](https://discourse.julialang.org/t/what-is-base-maybeview/58820/10 "2021-04-08T14:23:38Z")

</div>

Note that `Base.maybeview` is probably not used directly by `LHSFunctions`, you are showing already pre-compiled code. Some other public `Base` function was probably inlined putting `maybeview` there. It probably should not be used directly.
