I’m not the last man struggling with transfering from Requires
pkg to officially supported extension feature for Julia1.9+
.
With Requires
pkg we don’t care about where a function
and a struct
are defined because we can easily access them. But when switching to extension feature, we can not access those only defined inside an extension module
, to overcome this, one may need to pre-define an empty function or struct in the main pkg.
Let’s think about a using case. Suppose we have a Person
module, and this Person
has a lot of skills like Driving
, Writing
and Boxing
etc. If Driving
is really heavy, Person
only needs a small part of it, let’s say driving a car, then we can make Driving
as an [weakdeps]
. To use speedup
function only really implemented inside PersonDrivingExt
, we need to declare an empty speedup
in main pkg Person
like
module Person
export speedup
function speedup end
export DrivingCarStatus
mutable struct DrivingCarStatus
pos :: Coords
speed :: Real
function DrivingCarStatus(args...)
# some construction here like
new(initpos(xargs...), initspeed(yargs...))
end
end
end # module Person
and then import and implement speedup
in extension
module PersonDrivingExt
using Person, Driving
import Person: speedup, DrivingCarStatus
function speedup(status::DrivingCarStatus)
status.speed += 10.0
end
end # module PersonDrivingExt
So far it is acceptable doing those. Well some issues may occur :
- what if some types are only defined in
Driving
, like::Coords
insideDrivingCarStatus
- what if some functions are only defined in
Driving
, likeinitpos
andinitspeed
in constructor
when these issues happen, we can using Driving
inside Person
to solve, but it 's far away from extension’s original purpose. "Extension is mainly for extending functions already defined in main pkg"
is apparently not enough and not convinient.
On my opinion, extension is good for compatibility control for weakdeps, but if Requires
pkg can itself control the version of weakdeps, we could eassily merge some functionalities from heavy pkg dependency without changing much code while not introducing the extension feature from Julia1.9+. Treating a conditionaly-triggered-extension like an nature module would be nice, e.g.
module Person
# some Person's own functions and types here
end # module Person
# ======================================================
module PersonDrivingExt
using Person, Driving
export speedup, DrivingCarStatus
mutable struct DrivingCarStatus
pos :: Coords
speed :: Real
function DrivingCarStatus(args...)
# some construction here like
new(initpos(xargs...), initspeed(yargs...))
end
end
function speedup(status::DrivingCarStatus)
status.speed += 10.0
end
end # module PersonDrivingExt
What new opinions do you have ?