@@ -714,6 +714,41 @@ function invokelatest(@nospecialize(f), @nospecialize args...; kwargs...)
714714 Core. _apply_latest (inner)
715715end
716716
717+ """
718+ invoke_in_world(world, f, args...; kwargs...)
719+
720+ Call `f(args...; kwargs...)` in a fixed world age, `world`.
721+
722+ This is useful for infrastructure running in the user's Julia session which is
723+ not part of the user's program. For example, things related to the REPL, editor
724+ support libraries, etc. In these cases it can be useful to prevent unwanted
725+ method invalidation and recompilation latency, and to prevent the user from
726+ breaking supporting infrastructure by mistake.
727+
728+ The current world age can be queried using [`Base.get_world_counter()`](@ref)
729+ and stored for later use within the lifetime of the current Julia session, or
730+ when serializing and reloading the system image.
731+
732+ Technically, `invoke_in_world` will prevent any function called by `f` from
733+ being extended by the user during their Julia session. That is, generic
734+ function method tables seen by `f` (and any functions it calls) will be frozen
735+ as they existed at the given `world` age. In a sense, this is like the opposite
736+ of [`invokelatest`](@ref).
737+
738+ !!! note
739+ It is not valid to store world ages obtained in precompilation for later use.
740+ This is because precompilation generates a "parallel universe" where the
741+ world age refers to system state unrelated to the main Julia session.
742+ """
743+ function invoke_in_world (world:: Integer , @nospecialize (f), @nospecialize args... ; kwargs... )
744+ world = convert (UInt, world)
745+ if isempty (kwargs)
746+ return Core. _apply_in_world (world, f, args)
747+ end
748+ inner () = f (args... ; kwargs... )
749+ Core. _apply_in_world (world, inner)
750+ end
751+
717752# TODO : possibly make this an intrinsic
718753inferencebarrier (@nospecialize (x)) = Ref {Any} (x)[]
719754
0 commit comments