7

I'm struggling to understand parametric type creation in julia. I know that I can create a type with the following:

type EconData
    values
    dates::Array{Date}
    colnames::Array{ASCIIString}

    function EconData(values, dates, colnames)
    if size(values, 1) != size(dates, 1)
        error("Date/data dimension mismatch.")
    end
    if size(values, 2) != size(colnames, 2)
        error("Name/data dimension mismatch.")
    end
    new(values, dates, colnames)
    end
end

ed1 = EconData([1;2;3], [Date(2014,1), Date(2014,2), Date(2014,3)], ["series"])

However, I can't figure out how to specify how values will be typed. It seems reasonable to me to do something like

type EconData{T}
   values::Array{T}
   ...
   function EconData(values::Array{T}, dates, colnames)
   ...

However, this (and similar attempts) simply produce and error:

ERROR: `EconData{T}` has no method matching EconData{T}(::Array{Int64,1}, ::Array{Date,1}, ::Array{ASCIIString,2})

How can I specify the type of values?

2
  • This type looks very similar to one I've nearly finished building myself (and it is open-source and MIT license). So maybe I can save you some work here. Specifically, will your dates always be sorted in ascending order? If so, then you can take advantage of that sort in all methods that use your type. This is the basis of the type I constructed. Unfortunately I haven't gotten around to documenting it yet, but the source is on github. There are four options depending on whether data is vector or matrix and whether dates are unique. May 28, 2015 at 23:56
  • 1
    Also note that Array{T} is not a concrete type, because Array also takes a dimension parameter. See docs.julialang.org/en/release-0.3/manual/faq/… for why it matters.
    – tholy
    May 29, 2015 at 17:48

1 Answer 1

6

The answer is that things get funky with parametric types and inner constructors - in fact, I think its probably the most confusing thing in Julia. The immediate solution is to provide a suitable outer constructor:

using Dates

type EconData{T}
    values::Vector{T}
    dates::Array{Date}
    colnames::Array{ASCIIString}
    function EconData(values, dates, colnames)
        if size(values, 1) != size(dates, 1)
            error("Date/data dimension mismatch.")
        end
        if size(values, 2) != size(colnames, 2)
            error("Name/data dimension mismatch.")
        end
        new(values, dates, colnames)
    end
end
EconData{T}(v::Vector{T},d,n) = EconData{T}(v,d,n)

ed1 = EconData([1,2,3], [Date(2014,1), Date(2014,2), Date(2014,3)], ["series"])

What also would have worked is to have done

ed1 = EconData{Int}([1,2,3], [Date(2014,1), Date(2014,2), Date(2014,3)], ["series"])

My explanation might be wrong, but I think the probably is that there is no parametric type constructor method made by default, so you have to call the constructor for a specific instantiation of the type (my second version) or add the outer constructor yourself (first version).

Some other comments: you should be explicit about dimensions. i.e. if all your fields are vectors (1D), use Vector{T} or Array{T,1}, and if their are matrices (2D) use Matrix{T} or Array{T,2}. Make it parametric on the dimension if you need to. If you don't, slow code could be generated because functions using this type aren't really sure about the actual data structure until runtime, so will have lots of checks.

2
  • I'll second the first version, although personally I'd have done function EconData{T}((values::Vector{T}, dates::Vector{Date}, colnames::Array{ASCIIString}) on my inner constructor, and then have an outer constructor as follows: function EconData{T}((values::Vector{T}, dates::Vector{Date}, colnames::Array{ASCIIString}) = EconData{eltype(values)}(values, dates, colnames). It saves having to write the Type of values explicitly every time you construct one of them. May 28, 2015 at 23:51
  • In Julia 1.8.5 the proposed solution doesn't work, I'm getting an error: syntax: extra token "EconData" after end of expression Stacktrace: [1] top-level scope @ In[3]:3
    – vsoler
    Apr 10 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.