In C++, each type has an operator that converts it to another type. If this operator is not declared explicit, the conversion will be implicit, so if the compiler sees that converting one type to another is possible, it will do that without the user telling it to.
How do you achieve this behavior in Julia?
struct Vector2f
x::Number
y::Number
end
foo(vec::Vector2f) = # ...
I want to be able to call foo with a tuple of numbers, e.g. foo((1, 2)) should be the same as foo(Vector2f(1, 2)).
In C++, for this to be possible you would add a conversion operator which in Julia would be
julia> Vector2f(x::Tuple) = return Vector2f(x[1], x[2])
julia> convert(::Type{Vector2f}, x::Tuple) = return Vector2f(x)
But
julia> foo((1, 2))
ERROR: MethodError: no method matching foo(::Tuple{Int64, Int64})
Closest candidates are:
foo(::Vector2f)
@ Main REPL[2]:1
To be clear, I want any function taking a Vector2f to also be able to take a Tuple, without adding a method to the function, foo in this case.
Is there a way for this to be doable?