# Function methods based solely on element type of array of arbitrary dimension

**URL:** https://discourse.julialang.org/t/function-methods-based-solely-on-element-type-of-array-of-arbitrary-dimension/43653
**Category:** General Usage
**Created:** [July 25, 2020, 5:52am UTC](https://discourse.julialang.org/t/function-methods-based-solely-on-element-type-of-array-of-arbitrary-dimension/43653 "2020-07-25T05:52:37Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jerjorg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jerjorg/32/15100_2.png) [@jerjorg](https://discourse.julialang.org/u/jerjorg)
#### Post date: [July 25, 2020, 5:52am UTC](https://discourse.julialang.org/t/function-methods-based-solely-on-element-type-of-array-of-arbitrary-dimension/43653/1 "2020-07-25T05:52:37Z")

</div>

I’m new to Julia, so my question may be naive or obvious. I’ve searched the documentation and elsewhere but can’t find an answer. Is it possible to define a function in Julia that takes as an argument an array of arbitrary dimension and have different methods that are based only on the type of the elements in the array (and not the dimensions of the array)?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [July 25, 2020, 5:58am UTC](https://discourse.julialang.org/t/function-methods-based-solely-on-element-type-of-array-of-arbitrary-dimension/43653/2 "2020-07-25T05:58:27Z")

</div>

Probably not. Even if you could, you probably wouldn’t want to though. It’s hard to imagine anything you could do with an array that would be efficient if you didn’t know the dimension. (Note in case this is the confusion: dimension isn’t size. Dimension is Vector vs Matrix vs higher order). What’s your usecase for wanting this?

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [July 25, 2020, 6:05am UTC](https://discourse.julialang.org/t/function-methods-based-solely-on-element-type-of-array-of-arbitrary-dimension/43653/3 "2020-07-25T06:05:41Z")

</div>

Can you be more specific about the kinds of input and output you want?

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [July 25, 2020, 6:23am UTC](https://discourse.julialang.org/t/function-methods-based-solely-on-element-type-of-array-of-arbitrary-dimension/43653/4 "2020-07-25T06:23:24Z")

</div>

Something like?

```julia
f(::Array{Float64}) = 1
f(::Array{Int}) = 2

```

---

<div class="post-metadata">

### Author: ![jerjorg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jerjorg/32/15100_2.png) [@jerjorg](https://discourse.julialang.org/u/jerjorg)
#### Post date: [July 25, 2020, 7:08am UTC](https://discourse.julialang.org/t/function-methods-based-solely-on-element-type-of-array-of-arbitrary-dimension/43653/5 "2020-07-25T07:08:19Z")

</div>

Thanks for your responses. I’m not so much interested in this for efficiency as asserting that the arguments passed to the function make sense. For my use case, I have a function that takes an array as one argument and a 1D array of arrays as the other. I’d like to check that the array is in the array of arrays.

```julia
function contains(array::AbstractArray,arrays::AbstractArray)::Bool
    any([isapprox(array,arrays[i]) for i=1:length(arrays)])
end

```

The array can be scalars, vectors, matrices, …, and the elements of each should be real numbers.

---

<div class="post-metadata">

### Author: ![pthariensflame](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pthariensflame/32/5633_2.png) [@pthariensflame](https://discourse.julialang.org/u/pthariensflame)
#### Post date: [July 25, 2020, 7:15am UTC](https://discourse.julialang.org/t/function-methods-based-solely-on-element-type-of-array-of-arbitrary-dimension/43653/6 "2020-07-25T07:15:21Z")

</div>

You can do this fairly easily with a two-phase dispatch. All you need to do is have the “primary” method of this function call the function again with an extra parameter that it can then dispatch on, using the standard `eltype` function to compute it:

```julia
myfunc(arr::AbstractArray, other_params) = myfunc(arr, other_params, eltype(arr))

myfunc(arr::AbstractArray, other_params, elty::Type) = …general case code…

myfunc(arr::AbstractArray, other_params, elty::Type{<: Real}) = …code for when the elements are reals…

myfunc(arr::AbstractArray, other_params, elty::Type{<: Int}) = …code for when the elements are ints…

```

---

<div class="post-metadata">

### Author: ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)
#### Post date: [July 25, 2020, 12:10pm UTC](https://discourse.julialang.org/t/function-methods-based-solely-on-element-type-of-array-of-arbitrary-dimension/43653/7 "2020-07-25T12:10:11Z")

</div>

That is expressed as

```julia
function contains(base::AbstractArray, arrays::AbstractVector{<:AbstractArray{<:Real}})
    any(isapprox(base, a) for a in arrays)
end

```

or, more verbosely

```julia
function contains(base::AbstractArray, arrays::AbstractVector{A}) where {A<:AbstractArray{T} where T<:Real}
    any(isapprox(base, a) for a in arrays)
end

```

Note that materializing the generator in a vector is not necessary. Generators are iterable, so functions like `any`, `all`, `sum` etc. may work on them directly.

Enforcing very strict input type constraints is not very Julian, though. A style more likely to find is

```julia
contains(item, itr; eq = isequal) = any(x -> eq(item, x), itr)

```

Then, you can use it as `contains(array, arrays, eq = isapprox)`. As Julia is not statically compiled, neither type-constrained nor duck-typed function are going to produce compile-time errors anyway.
