# Type check Array vs UnitRange inside a struct

**URL:** https://discourse.julialang.org/t/type-check-array-vs-unitrange-inside-a-struct/43154
**Category:** General Usage
**Created:** [July 16, 2020, 3:07am UTC](https://discourse.julialang.org/t/type-check-array-vs-unitrange-inside-a-struct/43154 "2020-07-16T03:07:10Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)
#### Post date: [July 16, 2020, 3:07am UTC](https://discourse.julialang.org/t/type-check-array-vs-unitrange-inside-a-struct/43154/1 "2020-07-16T03:07:10Z")

</div>

Type checking seems to be off within structs when it comes to Arrays and UnitRanges. Defining a function with an argument that is of type Array forbids supplying a UnitRange as an input. However, when a struct is defined with one of its fields as an Array, it allows populating it with a UnitRange. Is this the intended behavior?

As an example:

```julia
#define a function
f(x::Array) = x 

#define a struct
struct F
    x::Array
end

display(f(1:3))

display(F(1:3))

```

f(1:3) gives a MethodError, whereas F(1:3) is accepted…

---

<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: [July 16, 2020, 3:45am UTC](https://discourse.julialang.org/t/type-check-array-vs-unitrange-inside-a-struct/43154/2 "2020-07-16T03:45:54Z")

</div>

This is behaving as expected. No conversion is done upon calling functions unless you define the methods that do the conversions.

When you define a struct without an inner constructor, Julia helpfully defines the constructor methods that do the conversion for you. You can turn this off by supplying an inner constructor.

```julia
julia> struct F
           x::Array
       end

julia> methods(F)
# 2 methods for type constructor:
[1] F(x::Array) in Main at REPL[1]:2
[2] F(x) in Main at REPL[1]:2

julia> @which F(1:3)
F(x) in Main at REPL[1]:2

julia> @code_lowered F(1:3)
CodeInfo(
1 ─ %1 = Main.F
│ %2 = Core.fieldtype(%1, 1)
│ %3 = Base.convert(%2, x)
│ %4 = %new(%1, %3)
└── return %4
)

```
