Hi, I am trying to learn multiple dispatch and I can’t figure out why the following code is not working.
module GeoLocation
using TimeZones
using DynamicQuantities
using Unitful: Unitful
using DocStringExtensions
export Location
"""
$(TYPEDEF)
"""
struct Location
"Latitude in degrees"
latitude::Quantity
"Longitude in degrees"
longitude::Quantity
"Altitude in meters"
altitude::Quantity
"Timezone of the location"
timezone::FixedTimeZone
end
# constructor accepting Float64 inputs
function Location(;
latitude::Float64,
longitude::Float64,
altitude::Float64 = 0.0,
timezone::FixedTimeZone = TimeZone("UTC"),
)
return Location(
Quantity(latitude, deg = 1),
Quantity(longitude, deg = 1),
Quantity(altitude, m = 1),
timezone,
)
end
# constructor accepting Quantity inputs
function Location(;
latitude::Quantity,
longitude::Quantity,
altitude::Quantity = Quantity(0.0, m = 1),
timezone::FixedTimeZone = TimeZone("UTC"),
)
return Location(latitude, longitude, altitude, timezone)
end
end
So basically I want to have two constructors, one for quantities and one for floats.
Quantities work fine:
julia> location1 = Location(latitude = Quantity(1.0, deg = 1), longitude = Quantity(1.0, deg = 1))
Location(1.0 , 1.0 , 0.0 , tz"UTC")
Floats fail:
julia> location1 = Location(latitude = 1.0, longitude = 1.0)
ERROR: TypeError: in keyword argument latitude, expected Quantity, got a value of type Float64
Stacktrace:
[1] top-level scope
@ REPL[17]:1