# Pass view to function

**URL:** <https://discourse.julialang.org/t/pass-view-to-function/5060>\
**Category:** New to Julia\
**Created:** [July 25, 2017, 6:10pm UTC](https://discourse.julialang.org/t/pass-view-to-function/5060 "2017-07-25T18:10:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxtremblay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxtremblay/32/1369_2.png) [@maxtremblay](https://discourse.julialang.org/u/maxtremblay)\
**Post date:** [July 25, 2017, 6:10pm UTC](https://discourse.julialang.org/t/pass-view-to-function/5060/1 "2017-07-25T18:10:20Z")

</div>

Hello,

Suppose I define a new type

```julia
struct foo
    x::Int
end

```

and I create a vector

`foos = [foo(3), foo(2), foo(9), foo(7)]`

Now, I want a function that I could call as

`bar(view(foos, 3:4))`

or as

`bar(view(foos, [2,4]))`

or as

`bar(view(foos, :))`.

What is the best way to achieve that?

Thank!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 25, 2017, 6:14pm UTC](https://discourse.julialang.org/t/pass-view-to-function/5060/2 "2017-07-25T18:14:04Z")

</div>

> [@maxtremblay](#):
>
> Now, I want a function that I could call as  
> bar(view(foos, 3:4))  
> or as  
> bar(view(foos, [2,4]))  
> or as  
> bar(view(foos, :)).  
> What is the best way to achieve that?

```julia
function bar(x)
  # x is the view
end

```

This auto-specializes on the type so there’s no performance left behind here.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [July 25, 2017, 6:14pm UTC](https://discourse.julialang.org/t/pass-view-to-function/5060/3 "2017-07-25T18:14:17Z")

</div>

Depends what you want to achieve. Possibly the best approach is to define `bar(x::AbstractVector)`. If you want to restrict it to `view`s specialize on `SubArray`. EDIT: of course if you don’t need to dispatch on the type you can just do what @ChrisRackauckas suggests above.

---

<div class="post-metadata">

**Author:** ![maxtremblay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxtremblay/32/1369_2.png) [@maxtremblay](https://discourse.julialang.org/u/maxtremblay)\
**Post date:** [July 25, 2017, 6:34pm UTC](https://discourse.julialang.org/t/pass-view-to-function/5060/4 "2017-07-25T18:34:37Z")

</div>

In fact, what I want to do is to use that function with an other type. So, I want something like

```julia
struct bar
    y::This is a view of a Vector{foo}
end

```

and to be able to call a constructor as I did in my original post.

Is it still a good idea to define it without specifying the type of `y`?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 25, 2017, 6:40pm UTC](https://discourse.julialang.org/t/pass-view-to-function/5060/5 "2017-07-25T18:40:54Z")

</div>

Try:

```julia
struct Bar{V <: AbstractVector{foo}}
  y::V 
end

```

which will let you create a `Bar` for any kind of vector of `foo` (including a regular `Vector{foo}` or a view into such a vector, etc.).

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 25, 2017, 6:40pm UTC](https://discourse.julialang.org/t/pass-view-to-function/5060/6 "2017-07-25T18:40:55Z")

</div>

> [@maxtremblay](#):
>
> and to be able to call a constructor as I did in my original post.  
> Is it still a good idea to define it without specifying the type of y?

This is now different. Types should be strictly typed in order to get performance. You should do something like:

```julia
struct bar{T}
    y::T
end

```

to leave it open but still get performance, or

```julia
struct bar{T<:AbstractArray}
    y::T
end

```

etc. to place a check (note, there’s no performance difference though, it’s just a safety check).

---

<div class="post-metadata">

**Author:** ![maxtremblay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxtremblay/32/1369_2.png) [@maxtremblay](https://discourse.julialang.org/u/maxtremblay)\
**Post date:** [July 25, 2017, 6:43pm UTC](https://discourse.julialang.org/t/pass-view-to-function/5060/7 "2017-07-25T18:43:38Z")

</div>

Good, thank for the answer. That is exactly what I needed.
