On Wednesday, October 22, 2014 10:14:41 AM UTC-4, Erik Schnetter wrote:
>
> What Stefan is probably trying to say:
>
> Instead of a function f(x, y) where x and y can have different types,
> write f{T}(x::T, y::T), so that x and y are forced to have the same
> type -- but this type can be any type. In this way, e.g. x+y can be
>
No, f(x,y) is fine. If x and y happen to have types such that x+y is fast,
then it will be fast. Remember that a *different* version of f gets
compiled for *every* set of argument types. So, when you call f(Int, Int)
it will compile a type-specialized version. You essentially never need to
declare argument types in functions for performance, only to control
dispatch.
However, you *do* need concrete types in your structures, which you can do
by type parameterization. e.g.
immutable CurveFP{T<:Number}
p::T
a::T
b::T
function CurveFP(p, a, b)
assert(mod(4*a^3+27*b^2,p) != 0)
new(p, a, b)
end
end