两个模块,都导出相同的名称 [英] Two Modules, both exporting the same name

查看:49
本文介绍了两个模块,都导出相同的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使用两个软件包: CorpusLoaders.jl WordNet.jl

There are two packages I want to use: CorpusLoaders.jl, and WordNet.jl

  • CorpusLoaders.SemCor导出sensekey(::SenseTaggedWord)
  • WordNet导出sensekey(::DB, ::Synset, ::Lemma)
  • CorpusLoaders.SemCor exports sensekey(::SenseTaggedWord)
  • WordNet exports sensekey(::DB, ::Synset, ::Lemma)

我想同时使用两种sensekey方法.

I want to use both sensekey methods.

例如

对于某些混合项目列表:mixedlist::Vector{Union{Tuple{SenseTaggedWord},Tuple{DB, Synset,Lemma}}. 也就是说,列表中的项是1元组的SenseTaggedWord和3元组的DBSynsetLemma的混合.

for some mixed list of items: mixedlist::Vector{Union{Tuple{SenseTaggedWord},Tuple{DB, Synset,Lemma}}. Ie the items in the list are a mixture of 1-tuples of SenseTaggedWord, and3 tuples of DB, Synset, and Lemma.

for item in mixedlist
    println(sensekey(item...)
end

应该工作. 这个示例有点滑稽,因为我为什么要像这样混合它们. 但是,希望它可以用来说明一般情况下的问题.

should work. This example is a little facetious, since why would I be mixing them like this. But, hopefully it serves for illustrating the problem in the general case.

尝试using CorpusLoaders.SemCor, WordNet将两个结果都放入WARNING: both WordNet and Semcor export "sensekey"; uses of it in module Main must be qualified.

Trying to using CorpusLoaders.SemCor, WordNet to bring in both results in WARNING: both WordNet and Semcor export "sensekey"; uses of it in module Main must be qualified.

手动导入两者:import CorpusLoaders.SemCor.sensekey; import WordNet.sensekey导致WARNING: ignoring conflicting import of Semcor.sensekey into Main

可以做什么?我想要它们两者,并且由于多重调度,它们并没有真正的冲突.

鉴于CorpusLoaders.jl是我正在编写的程序包,我确实有更多选择,因为我可以使CorpusLoaders.jl依赖WordNet.jl. 如果可以,那么我可以在CorpusLoaders.jl

Given that CorpusLoaders.jl is a package I am writing I do have a few more options, since I could make my CorpusLoaders.jl depend on WordNet.jl. If I did do than then I could say in CorpusLoaders.jl

 import WordNet
 function WordNet.sensekey(s::SenseTaggedWord)...

,这将使它们都起作用. 但这意味着需要WordNet作为CorpusLoaders的依赖项.

and that would make them both work. But it would mean requiring WordNet as a dependency of CorpusLoaders.

我想知道如何为包装的使用者解决问题,而不是作为包装的创建者.

And I want to know how to solve the problem for a consumer of the packages -- not as the creator of the packages.

推荐答案

在这种情况下,功能不会发生冲突,但是通常无法保证.稍后加载的程序包可能会将方法添加到将要冲突的功能之一中.因此,要对两个软件包都使用sensekey,还需要一些其他保证和限制.

In this case, the functions do not conflict, but in general that is impossible to guarantee. It could be the case that a package loaded later will add methods to one of the functions that will conflict. So to be able to use the sensekey for both packages requires some additional guarantees and restrictions.

执行此操作的一种方法是忽略两个程序包的sensekey,而是提供自己的程序包,并分派到正确的程序包中:

One way to do this is to ignore both package's sensekey, and instead provide your own, dispatching to the correct package:

sensekey(x) = CorpusLoaders.sensekey(x)
sensekey(x, y, z) = WordNet.sensekey(x,y,z)

这篇关于两个模块,都导出相同的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆