This code still have a bugs and I don’t know how to remove it.
# Type definiton
struct GF{p, T} <: Number where {p, T<:Integer}
rep::T # Representation: intyger which holds the value of element in GF(p)
function GF{p, T}(x::Integer) where {p, T<:Integer}
return new(mod(x, p))
end
end
GF{p}(x::T) where {p, T<:Integer} = GF{p, T}(x)
import Base: convert, inv, one, promote_rule, show, zero
function call(::Type{GF{p}}, x::Integer) where p
if !isprime(p)
throw(ArgumentError("p must be prime"))
end
return GF{p, typeof(x)}(mod(x, p))
end
convert(::Type{GF{p, T}}, x::Integer) where {p, T} = GF{p}(x)
convert(::Type{GF{p}}, x::Integer) where p = GF{p}(x)
convert(::Type{GF{p, T}}, x::GF{p}) where {p, T} = GF{p, T}(x.rep)
promote_rule(::Type{GF{p, T1}}, ::Type{T2}) where {p, T1, T2<:Integer} = GF{p, promote_type(T1, T2)}
# To be able show and print GF(p)
show(io::IO, x::GF) = show(io, x.rep)
Since isprime is now not defined in global scope GF{4}(5) is valid expression and return 0, of type GF{4, Int64}. I confused, why there is no error of using undefined !isprime(p) in conditional statement?
Second question: why GF{4,5} works and what this expression mean?