I do not understand (numeric?) type parameters, and the manual has not
enlightened me.
As an exercise, I'm trying to write a ModInt type.

I found https://github.com/JuliaLang/julia/blob/master/examples/modint.jl ,
which does work.

However, I don't understand what the difference is between the version used
there and the version I was trying.

The example:
~~~

immutable ModInt{n} <: Integer
    k::Int

    ModInt(k) = new(mod(k,n))
end

~~~

My failing attempt:
~~~
immutable ModInt{n} <: Integer
           k::Int
           ModInt(k,n) = new{n}(mod(k,n))
end
~~~

At first, I was confused about how the example knows what `n` is in the
constructor.  I realize that this is constructed as `ModInt{2}(5)`, but the
`n` is just magically shared?
This part is clarified in the manual, if you assume that numeric type
parameters work exactly as normal ones. (
http://docs.julialang.org/en/release-0.2/manual/constructors/#parametric-constructors
)

In the my attempted version, the type is accepted, but constructing things
doesn't work:
~~~
julia> five = ModInt(5,2)
ERROR: no method ModInt{n}(Int64, Int64)

julia> five = ModInt{2}(5)
ERROR: no method ModInt{2}(Int64)
~~~

The second instantiation works for the example, which is what I expected.
However, I still don't understand why the first instantiation doesn't work
for my version. What does the compiler think is going on?

Thanks,
Leah

Reply via email to