Leah-- that is very helpful, thanks! 

I rewrote the code testnestparam with parameters everywhere (see below), 
and now it works. It does not work, however, if I define test0(i::SDToken) 
= nothing (method not found error).  I don't understand why that is.  Isn't 
any instance of SDToken{K,D} for a specific choice {K,D} a subtype of 
abstract unparametrized SDToken?   In this case, why can't Julia match 
test0 when it is unparametrized?

More generally, is there a writeup on parametrized types that goes into 
more detail than the manual and has many examples?  I keep making mistakes 
with them because my understanding is incomplete.

Thanks,
Steve

module testnestparam

immutable IntSemiToken
    address::Int
end

immutable Token{T, S}
    container::T
    semitoken::S
end

# take a token apart
semi(i::Token) = i.semitoken
container(i::Token) = i.container

# put a token back together
assemble(m, s) = Token(m,s)

type SortedDict{K, D} <: Associative{K,D}
    bt::Dict{K,D}
end

typealias SDToken{K,D} Token{SortedDict{K,D}, IntSemiToken}

sdtoken_construct{K,D}(m::SortedDict{K,D}, int1::Int) = 
    SDToken{K,D}(m, IntSemiToken(int1))

test0{K,D}(i::SDToken{K,D}) = nothing
# test0(i::SDToken) = nothing  # This variant fails.  Why?

function test1()
    s = SortedDict([1=>"a",2=>"b"])
    t = sdtoken_construct(s, 0)
    t2 = assemble(t.container, t.semitoken)
    println("typeof(t) = ", typeof(t))
    println("typeof(t2) = ", typeof(t2))
    println("methods(test0) = ", methods(test0))
    test0(t)
    test0(t2) 
end

end






On Tuesday, October 7, 2014 12:58:30 PM UTC-4, Leah Hanson wrote:
>
> You need to fully specify TDict when using it's constructor:
>
> ~~~
> julia> module testnestparam2
>
>        immutable Token{S,T}
>            m::S
>            t::T
>        end
>
>        typealias TDict{K,D} Token{Dict{K,D}, Int}
>
>        function makeADict()
>            a = TDict{Int64,String}([1=>"a",2=>"c"], 5)
>        end
>
>        end
> Warning: replacing module testnestparam2
>
> julia> testnestparam2.makeADict()
> Token{Dict{Int64,String},Int64}([2=>"c",1=>"a"],5)
> ~~~
>
> On Tue, Oct 7, 2014 at 11:41 AM, <[email protected] <javascript:>> 
> wrote:
>
>> Oops, sorry, in my last posting there was obviously an error in my code 
>> because I used the wrong syntax to call an array constructor.  Here is 
>> another example where now I am using the correct constructor syntax (I 
>> think?).  But I still get the same error message concerning 'apply'.
>>
>> module testnestparam2
>>
>> immutable Token{S,T}
>>     m::S
>>     t::T
>> end
>>
>> typealias TDict{K,D} Token{Dict{K,D}, Int}
>>
>> function makeADict()
>>     a = TDict([1=>"a",2=>"c"], 5)
>> end
>>
>> end
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> On Tuesday, October 7, 2014 12:29:49 PM UTC-4, [email protected] wrote:
>>>
>>> Here is a very short Julia code that gives a mysterious error message in 
>>> the latest build (952).  Is it an error in my code or a bug in Julia?  (In 
>>> addition to the answer to this question, I would also like help with the 
>>> code design question in my previous posting of two hours ago.)
>>>
>>> Thanks,
>>> Steve
>>>
>>> module testnestparam2
>>>
>>> typealias ADict{K,D} Array{Dict{K,D},1}
>>>
>>> function makeADict()
>>>     a = ADict([[-9=>"a"],[15=>"b"]]) #line 6
>>>     println("a[1][-9] = ", a[1][-9])
>>> end
>>>
>>> end
>>>
>>> julia> testnestparam2.makeADict()
>>> ERROR: type: makeADict: in apply, expected Function, got 
>>> Type{Array{Dict{K,D},1
>>> }
>>>  in makeADict at c:\Users\vavasis\Documents\Projects\qmg21\julia\
>>> testnestparam2
>>> jl:6
>>>
>>>
>>>
>>> On Tuesday, October 7, 2014 10:05:09 AM UTC-4, [email protected] 
>>> wrote:
>>>>
>>>> Thanks for the explanation!  Although I think I understand your answer, 
>>>> I'm not sure how to define the types to accomplish my goal.
>>>>
>>>> * a Token should have two parts, a container and a semitoken, both 
>>>> abstract
>>>>
>>>> * an SDToken should be a specialization of Token in which the container 
>>>> portion is SortedDict{K,D} for a specific K,D and the semitoken portion is 
>>>> IntSemiToken (both parts concrete)
>>>>
>>>> * Some functions take SDToken's as input.  These should be generic, 
>>>> i.e., work for all choices of K,D.
>>>>
>>>> * There are also generic functions that return SDTokens as output (in 
>>>> which K and D are determined from the input args).
>>>>
>>>> Can I write typealias SDToken{K,D} Token{SortedDict{K,D},IntSemiToken}?  
>>>> I tried this and got an unexpected error message:
>>>>
>>>> ERROR: type: test1: in apply, expected Function, got 
>>>> Type{Token{SortedDict{K,D},
>>>> IntSemiToken}}
>>>>  in test1 at c:\Users\vavasis\Documents\Projects\qmg21\julia\
>>>> testnestparam.jl:32
>>>>
>>>> (No 'apply' in my code!)  And in any case, even if I could write this, 
>>>> I'm not sure it solves the problem.
>>>>
>>>> -- Steve
>>>>
>>>>
>>>>
>>>> On Tuesday, October 7, 2014 2:15:01 AM UTC-4, Jutho wrote:
>>>>>
>>>>> I don't see the problem regarding point one.
>>>>>
>>>>> A type with parameters, as your SortedDict{D,K}, becomes an abstract 
>>>>> type when the parameters are unspecified, e.g. SortedDict, but this is 
>>>>> indeed printed/formatted with unspecified parameters put back (I guess 
>>>>> with 
>>>>> the name as you defined them), e.g. as SortedDict{D,K}.
>>>>>
>>>>> Regarding point 2, this relates to the invariance of parametric types. 
>>>>>
>>>>> isa(t2,Token{SortedDict, IntSemiToken})
>>>>> will return false, because
>>>>> Token{SortedDict{Int64,ASCIIString},IntSemiToken} <: 
>>>>> Token{SortedDict, IntSemiToken}
>>>>> is false. There are many discussions regarding this both on the forum 
>>>>> and I guess in the manual. Search for invariance versus covariance of 
>>>>> parametric types.
>>>>>
>>>>>
>>>>> Op dinsdag 7 oktober 2014 04:42:12 UTC+2 schreef [email protected]:
>>>>>>
>>>>>> The code below is an excerpt from a more complicated code I am 
>>>>>> writing.  It contains a parametrized type in which the parameter is 
>>>>>> itself 
>>>>>> another parametrized type. I have attached the printout (0.4.0-dev+323). 
>>>>>>  
>>>>>> Notice the error message at the end.  My questions are:
>>>>>>
>>>>>> (1) How is it possible that the type of t includes dummy parameters K 
>>>>>> and D, which aren't real types at all?
>>>>>>
>>>>>> (2) Why is Julia not able to match t2 but it is able to match t to 
>>>>>> the signature of test0()?  
>>>>>>
>>>>>> Thanks,
>>>>>> Steve Vavasis
>>>>>>
>>>>>>
>>>>>> julia> testnestparam.test1()
>>>>>> typeof(t) = Token{SortedDict{K,D},IntSemiToken}
>>>>>> typeof(t2) = Token{SortedDict{Int64,ASCIIString},IntSemiToken}
>>>>>> methods(test0) = # 1 method for generic function "test0":
>>>>>> test0(i::Token{SortedDict{K,D},IntSemiToken}) at 
>>>>>> c:\Users\vavasis\Documents\Projects\qmg21\julia\testnestparam.jl:28
>>>>>> ERROR: `test0` has no method matching test0(::Token{SortedDict{
>>>>>> Int64,ASCIIString},IntSemiToken})
>>>>>>  in test1 at c:\Users\vavasis\Documents\Projects\qmg21\julia\
>>>>>> testnestparam.jl:38
>>>>>>
>>>>>>
>>>>>> module testnestparam
>>>>>>
>>>>>> immutable IntSemiToken
>>>>>>     address::Int
>>>>>> end
>>>>>>
>>>>>> immutable Token{T, S}
>>>>>>     container::T
>>>>>>     semitoken::S
>>>>>> end
>>>>>>
>>>>>> # take a token apart
>>>>>> semi(i::Token) = i.semitoken
>>>>>> container(i::Token) = i.container
>>>>>>
>>>>>> # put a token back together
>>>>>> assemble(m, s) = Token(m,s)
>>>>>>
>>>>>> type SortedDict{K, D} <: Associative{K,D}
>>>>>>     bt::Dict{K,D}
>>>>>> end
>>>>>>
>>>>>> typealias SDToken Token{SortedDict, IntSemiToken}
>>>>>>
>>>>>> sdtoken_construct(m::SortedDict,int1::Int) = 
>>>>>>     SDToken(m, IntSemiToken(int1))
>>>>>>
>>>>>> test0(i::SDToken) = nothing
>>>>>>
>>>>>> function test1()
>>>>>>     s = SortedDict([1=>"a",2=>"b"])
>>>>>>     t = sdtoken_construct(s, 0)
>>>>>>     t2 = assemble(t.container, t.semitoken)
>>>>>>     println("typeof(t) = ", typeof(t))
>>>>>>     println("typeof(t2) = ", typeof(t2))
>>>>>>     println("methods(test0) = ", methods(test0))
>>>>>>     test0(t)
>>>>>>     test0(t2) #Line 38
>>>>>> end
>>>>>>
>>>>>> end
>>>>>>
>>>>>>
>

Reply via email to