# Is there such a thing as an implicit conversion from a matrix view?

**URL:** <https://discourse.julialang.org/t/is-there-such-a-thing-as-an-implicit-conversion-from-a-matrix-view/110332>\
**Category:** General Usage\
**Created:** [February 17, 2024, 3:52am UTC](https://discourse.julialang.org/t/is-there-such-a-thing-as-an-implicit-conversion-from-a-matrix-view/110332 "2024-02-17T03:52:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [February 17, 2024, 3:52am UTC](https://discourse.julialang.org/t/is-there-such-a-thing-as-an-implicit-conversion-from-a-matrix-view/110332/1 "2024-02-17T03:52:07Z")

</div>

I am trying to construct a struct that refers to a view of a vector. However, there appears to be an implicit conversion, and I end up with a copy of the “view”.

```julia
mutable struct A{
    IT,
    MBT, # matrix-entry buffer type
    IJT, # index buffer type
} 
    buffer_length::IT
    matbuffer::MBT
    rowbuffer::IJT
end

function _task_local_assembler(a::AT, buffer_range) where {AT}
    buffer_length = maximum(buffer_range) - minimum(buffer_range) + 1
    matbuffer = view(a.matbuffer, buffer_range)
    rowbuffer = view(a.rowbuffer, buffer_range)
    @show typeof(matbuffer)
    _a = AT(
        buffer_length,
        matbuffer,
        rowbuffer
    )
    @show typeof(_a)
    return _a
end

a = A(1, zeros(Float64, 10), zeros(Int, 10))

_a = _task_local_assembler(a, 1:5)

a.matbuffer .= -1
@show a 
@show _a

```

On Windows 11, Julia 1.10, I end up with:

```julia
julia> include(raw"playo.jl")
typeof(matbuffer) = SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true}
typeof(_a) = A{Int64, Vector{Float64}, Vector{Int64}}
a = A{Int64, Vector{Float64}, Vector{Int64}}(1, [-1.00000e+00, -1.00000e+00, -1.00000e+00, -1.00000e+00, -1.00000e+00, -1.00000e+00, -1.00000e+00, -1.00000e+00, -1.00000e+00, -1.00000e+00], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])   
_a = A{Int64, Vector{Float64}, Vector{Int64}}(5, [0.00000e+00, 0.00000e+00, 0.00000e+00, 0.00000e+00, 0.00000e+00], [0, 0, 0, 0, 0])
A{Int64, Vector{Float64}, Vector{Int64}}(5, [0.00000e+00, 0.00000e+00, 0.00000e+00, 0.00000e+00, 0.00000e+00], [0, 0, 0, 0, 0])

```

---

<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:** [February 17, 2024, 4:04am UTC](https://discourse.julialang.org/t/is-there-such-a-thing-as-an-implicit-conversion-from-a-matrix-view/110332/2 "2024-02-17T04:04:29Z")

</div>

Your `AT` constructor is based on the type of `a` — parameters and all. And the parameters of `a` are `Vector`s. And the default constructors do a convert to the field types for you — even for parameterized fields, when they are given. And there is a conversion defined from SubArray to Vector.

So, yes.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [February 17, 2024, 4:08am UTC](https://discourse.julialang.org/t/is-there-such-a-thing-as-an-implicit-conversion-from-a-matrix-view/110332/3 "2024-02-17T04:08:36Z")

</div>

Thanks. Can I prevent that from happening?

---

<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:** [February 17, 2024, 4:20am UTC](https://discourse.julialang.org/t/is-there-such-a-thing-as-an-implicit-conversion-from-a-matrix-view/110332/4 "2024-02-17T04:20:57Z")

</div>

Many ways. One simple one is to just use `A` directly instead of the captured and fully parameterized `AT`.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [February 17, 2024, 4:25am UTC](https://discourse.julialang.org/t/is-there-such-a-thing-as-an-implicit-conversion-from-a-matrix-view/110332/5 "2024-02-17T04:25:47Z")

</div>

I do not know what the actual type is. I only know the `a` that is passed in. The function should create an object of type similar to `typeof(a)`, but with those views.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [February 17, 2024, 8:26am UTC](https://discourse.julialang.org/t/is-there-such-a-thing-as-an-implicit-conversion-from-a-matrix-view/110332/6 "2024-02-17T08:26:36Z")

</div>

@mbauman is suggesting to replace `_a = AT(...)` with `_a = A(...)`. Is this not doing what you need?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [February 17, 2024, 1:37pm UTC](https://discourse.julialang.org/t/is-there-such-a-thing-as-an-implicit-conversion-from-a-matrix-view/110332/7 "2024-02-17T13:37:13Z")

</div>

The thing is: I do not know at the call site what A is. I only know `a`, and hence `typeof(a)`…

---

<div class="post-metadata">

**Author:** ![jondeuce](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jondeuce/32/16378_2.png) [@jondeuce](https://discourse.julialang.org/u/jondeuce)\
**Post date:** [February 18, 2024, 2:59am UTC](https://discourse.julialang.org/t/is-there-such-a-thing-as-an-implicit-conversion-from-a-matrix-view/110332/8 "2024-02-18T02:59:34Z")

</div>

You’re looking for something like `constructorof` from [`ConstructionBase.jl`](https://github.com/JuliaObjects/ConstructionBase.jl/tree/master), e.g. [the default method](https://github.com/JuliaObjects/ConstructionBase.jl/blob/8e3e773c7ee65a664fae6af11dd0ccdc5ef6478a/src/ConstructionBase.jl#L29) will give you `A` from `a`:

```julia
@generated function constructorof(::Type{T}) where T
    getfield(parentmodule(T), nameof(T))
end

```

You can also overload this function for your own types.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [February 18, 2024, 4:57am UTC](https://discourse.julialang.org/t/is-there-such-a-thing-as-an-implicit-conversion-from-a-matrix-view/110332/9 "2024-02-18T04:57:22Z")

</div>

> [@jondeuce](#):
>
> ```julia
> @generated function constructorof(::Type{T}) where T
> getfield(parentmodule(T), nameof(T))
> end
> 
> ```

Nice!!!
