0

If I have a DUB-based project with an optional dependency, how to I make it so that some code (be it modules or a version(...){...} block) only gets compiled if the dependency is chosen? Is there a way I can make a version identifier defined based on whether the dependency is present or absent?

I have already read about the "optional" and "default" attributes of the "dependency" tag as documented here. This allows me to define a dependency as optional, but it lacks teeth if I can't version my code to reflect the dependencies chosen.

4

2 回答 2

2

也许你可以,我不确定,但似乎你可以使用这样的东西:

version(Have_name_of_dependency)

因此,如果您的可选依赖项是 sdlang-d 您可以使用

Have_sdlang_d

和这里一样

这是基于部分配音代码

于 2017-08-15T19:43:35.037 回答
0

除了使用version(foo)块并要求人们versions: "foo"在他们的 dub.sdl 中使用之外,您还有另一种选择。这很笨拙,但它会起作用。

如果您的代码是模板,则可以将相关内容包装在:

template HasVibe() 
{
    enum HasVibe = 
        is(typeof(() { import vibe.d; } ));
}

template Foo(T) if (HasVibe!())
{
    // your code here
}

如果不是模板,可以转成模板:

template log() if (HasVibe!())
{
    import vibe.core.log : logDebug;
    alias log = logDebug;
}

未经测试,但它可能会在紧要关头有所帮助。

于 2017-08-17T01:43:30.683 回答