From ripley at stats.ox.ac.uk  Sun Jan 15 08:34:36 2012
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Sun, 15 Jan 2012 07:34:36 +0000
Subject: [Rd] A: ImportFrom(B, ...)  -- B: ImportFrom(A, ...)
In-Reply-To: <20241.63919.31556.448651@stat.math.ethz.ch>
References: <20241.63919.31556.448651@stat.math.ethz.ch>
Message-ID: <4F12818C.3050700@stats.ox.ac.uk>

It is necessary (in most cases, and as implemented).

When R goes to load the namespace of A, it has to find the objects you 
wish to import from B.  Remember that objects in R are not standalone: 
they may have shared components and functions have environments, so the 
only way to so this is to load the whole of their environment, the 
namespace of B.  That would lead to a cycle.

importFrom() differs from import() only in what it copies into A's 
'import' environment.

Remember too that namespaces can re-export imports (e.g. S4 generics), 
and that loading a namespace may load compiled code which may have 
dependencies (your examples do) for which order may matter.

On 14/01/2012 21:54, Martin Maechler wrote:
> If I have a package A  whose NAMESPACE has
>   ImportFrom(B,<B-syms>))
>
> and a package B  whose NAMESPACE has
>   ImportFrom(A,<A-syms>)
>
> and we can assume that<A-syms>  and<B-syms>  are disjoint.
> The clue would be that I only import relatively little from
> one namespace to the other.
>
> I currently get an error
>
>    Error in loadNamespace(imp[[1L]], c(lib.loc, .libPaths())) :
>      cyclic namespace dependency detected when loading ?A?,
>      already loading ?B?, ?A?
>
>
> but isn't this an unnecessary (and too limiting) restriction?
>
> The use case would be packages Rmpfr and gmp, both for high
> precision arithmetic, and it would be considerably more natural
> if each could import some facilities from the other, instead of
> having an asymmetry  A -->  B  (or B -->  A).
>
> Martin
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] A: ImportFrom(B, ...) -- B: ImportFrom(A, ...)
Prof Brian Ripley