Fail compilation with helpful message when trying to register class by supertype it does not inherit from

This commit is contained in:
Mike Bierlee 2021-04-29 22:13:29 +03:00
parent d0728d5025
commit edfa5cdffb
2 changed files with 10 additions and 0 deletions

View file

@ -1,5 +1,8 @@
Poodinis Changelog Poodinis Changelog
================== ==================
**Version 8.1.0-beta.4**
* ADD compilation failure when trying to register a class by a supertype it does not inherit from. (#31)
**Version 8.1.0-beta.3** **Version 8.1.0-beta.3**
* CHANGE injection initializers to be defined as a registration scope instead of via Container.register(). See initializedBy(). * CHANGE injection initializers to be defined as a registration scope instead of via Container.register(). See initializedBy().
* ADD initializedOnceBy() to create singleton instances via injection initializer. * ADD initializedOnceBy() to create singleton instances via injection initializer.

View file

@ -25,6 +25,7 @@ import std.string;
import std.algorithm; import std.algorithm;
import std.concurrency; import std.concurrency;
import std.traits; import std.traits;
import std.meta : AliasSeq;
debug { debug {
import std.stdio; import std.stdio;
@ -194,6 +195,12 @@ synchronized class DependencyContainer {
return newRegistration; return newRegistration;
} }
public Registration register(SuperType, ConcreteType)(RegistrationOption options = RegistrationOption.none)
if (!is(SuperType == ConcreteType) && !is(BaseTypeTuple!ConcreteType == AliasSeq!(Object, SuperType)) && !is(BaseTypeTuple!ConcreteType == AliasSeq!(SuperType))) {
pragma(msg, "Cannot register dependency: ", ConcreteType, " is not derived from ", SuperType);
static assert(0, "Cannot register dependency");
}
private bool hasOption(OptionType)(OptionType options, OptionType persistentOptions, OptionType option) { private bool hasOption(OptionType)(OptionType options, OptionType persistentOptions, OptionType option) {
return ((options | persistentOptions) & option) != 0; return ((options | persistentOptions) & option) != 0;
} }