I am new to julia (just started couple of days ago). I would like to ask, if is there a way of defining the template function in julia with no input parameters similiar to this in C++.
#include <iostream>
#include <iomanip>
#include <type_traits>
#include <cstdint>
template<typename eT>
eT fun()
{
eT x;
if (std::is_same<eT,double>::value)
{
std::cout << "returns double\n";
} else if ( std::is_same<eT,int>::value ) {
std::cout << "returns int\n";
} else {
std::cout << "...\n";
}
return static_cast<eT>(1.000);
}
int main()
{
int i = fun<int>();
double d = fun<double>();
return 0;
}
So, the question is how Can i Do it properly in Julia.
function test_template() where (T<:Real)
x::T = round( T , 1.0)
if x isa Float64
println("Data type is Float64")
elseif x isa Float32
println("Data type is Float32")
elseif x isa Int64
println("Data type is Int64")
else
println("Data type is unknown")
end
return x
end
Because, when I call it this way I get
julia> test_template()
ERROR: UndefVarError: T not defined
Thanks you all for sharing your knowledge.