I wrote about this a few days ago:
https://groups.google.com/d/msg/julia-users/jUMu9A3QKQQ/-ZShKvLXAwAJ

It's inner vs outer constructors.  Basically you have to pass in the
type as a normal function argument, like e.g. `Array(Int,3)` does, or
use `call` overloading:

julia> Array{Int}(3)
3-element Array{Int64,1}:
 0
 0
 0

julia> @which Array{Int}(3)
call{T}(::Type{Array{T,N}}, m::Integer) at essentials.jl:190

julia> @less Array{Int}(3)

call{T}(::Type{Array{T}}, m::Integer) =
    ccall(:jl_alloc_array_1d, Array{T,1}, (Any,Int), Array{T,1}, m)

(or on 0.5
julia> @which Array{Int}(3)
(::Type{Array{T,N<:Any}}){T}(m::Int64) at boot.jl:315)


On Sun, 2016-04-17 at 18:12, Davide Lasagna <lasagnadav...@gmail.com> wrote:
> Hi,
>
> I am defining a type with two parameters, see code below as a
> demonstration. I can, of course, use the default constructor with all
> parameters explicitly specified. However, I would like to have an extra
> constructor in which I have to specify only the first parameter, and have
> the second take a reasonable default.
>
> # define the type
> type foo{T<:Number, S<:Integer}
>     x::Vector{T}
>     y::Vector{S}
>     isfoo::Bool
>     # default constructor does not take arguments, and
>     # initialises the internal fields appropriately.
>     foo() = new(T[], S[0], true)
> end
>
> # with default constructor you must specify all parameters
> f = foo{Int64, Int32}()
>
> # want to define a constructor with default second parameter, e.g.:
> foo{T}() = foo{T, Int32}()
>
> # in order to do things like
> b = foo{Int64}()
>
> The extra method definition results in the error
>
> WARNING: static parameter T does not occur in signature for call at none:1.
> The method will not be callable.
> foo{T,S}
>
> and the constructor cannot be of course utilised.
>
> Does julia allow anything like this? It might not be possible at all, so I
> am interested in ways to achieve the same end in a different way.
>
> Thanks

Reply via email to