假设有一个调用 bar::foo()
函数的语句,我希望能够以编程方式切换到包 hello
,使得相同的语法可以调用 hello::foo()
。
示例:
- Let's say I have three packages,
parentPkg
,childPkg1
andchildPkg2
. - In
parentPkg
I have a call to functionchildPkg1::foo()
foo()
is also a function inchildPkg2
I would like to be able, in
parentPkg
to use the::
operator to callfoo()
but to programatically switch the package name. Something like:dummy_pkg_name = ifelse(scenario=="child1", "childPkg1", "childPkg2") dummy_pkg_name::foo()
这可行吗?我该如何实现它?
一些背景信息
parentPkg
是一个与 Web 应用程序交互的函数,接受某些请求和数据,并根据场景从不同的统计模型返回结果。每个场景都非常复杂,parentPkg
无法将所有内容概括。因此,childPkg1
和 childPkg2
(实际上还有3和4)是一些子包,用于处理数据清理和每个场景的各种替代方案,但返回相同的值类别。
思路是:parentPkg
根据场景切换到相应的子包,并调用所有必要的函数,而无需为每个子包编写相同的序列,只需稍微不同的 ::
调用即可。