Type check Array vs UnitRange inside a struct

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> 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
)
2 Likes