Search code examples
functiontypesjuliafunction-call

Call a Function with parametric type while specifying the Type (Julia 0.5)


(I am referring to Julia 0.5) Lets say I define a function:

f{T<:Real}(x::T=one(T), y::T=one(T)) = x+y And I want to call it while specifying the type, but without specifying it via the arguments. E.g. I want to do: f{Float64}() In the console this gives the error: ERROR: TypeError: Type{...} expression: expected Type{T}, got #f So, is it possible to call any function (besides constructors for parametric types) using the {} syntax during the function call?

EDIT: The reason I came up with this question is because I wanted to use default arguments but also specify the Type at the same time, something like f{BigFloat}().


Solution

  • If you don't mind writing f(BigFloat) instead of f{BigFloat}(), you could change your function definition to:

    f{T}(args::T...) = f(T, args...)
    f{T<:Real}(::Type{T}, x::T=one(T), y::T=one(T)) = x+y
    

    Which allows you to specify a type if necessary while still letting you write f(1,2).