On Sun, Oct 11, 2015 at 5:57 PM, Andrei Zh <[email protected]> wrote:
> Let's consider 2 types with inner constructors:
>
> type Foo
>     x::Array{Int,1}
>
>
>     Foo() = Foo(zeros(Int, 10))
> end
>
> type Bar{T}
>     x::Array{T,1}
>
>
>     Bar() = Bar(zeros(T, 10))
> end
>
> The only difference between them is that `Bar` has type parameter while
> `Foo` doesn't. I'd expect their inner constructors behave the same way, but
> `Bar` turns to ignore inner constructor definition:
>
> julia> methods(Foo)
>  3-element Array{Any,1}:
>   call(::Type{Foo}) at /home/<username>/work/playground/contructors.jl:5
>   call{T}(::Type{T}, arg) at essentials.jl:56
>   call{T}(::Type{T}, args...) at essentials.jl:57
>
>
>  julia> methods(Bar)
>  2-element Array{Any,1}:
>   call{T}(::Type{T}, arg) at essentials.jl:56
>   call{T}(::Type{T}, args...) at essentials.jl:57
>
> Is it a bug or a feature and how to make `Bar` recognize inner constructor
> (outer constructor is not an option here since it uses type parameter `T` in
> constructor body, but not in its constructor)?
>

As you pointed out, the inner constructor can access the type
parameter and therefore the type parameter needs to be specified in
order to call it.

julia> methods(Bar{Int})
3-element Array{Any,1}:
 call{T}(::Type{Bar{T}}) at none:3
 call{T}(::Type{T}, arg) at essentials.jl:55
 call{T}(::Type{T}, args...) at essentials.jl:56

Reply via email to