Question about template argument matching with alias this

Johannes Loher johannes.loher at fg4f.de
Sun Jul 29 23:03:27 UTC 2018


On Sunday, 29 July 2018 at 20:51:45 UTC, Alex wrote:
> Do you mean something like this?
> [...]

Yeah, I know that it possible to implement the template like 
this, but that is not the point here. I would like to know why it 
does not work the way I described it. To me it seems very 
strange, that `S : T` has different semantics in `is` expressions 
and as template parameters.

My actual problem is the following: I would like to use the 
dependency injection framework poodinis [1] in conjuction with 
the mocking capabilities from unit-threaded [2]. My code would 
look something like the following:

```
import poodinis : DependencyContainer;
import unit_threaded.mock : mock;

interface SomeInterface
{
}

unittest
{
     auto myMock = mock!SomeInterface;
     alias MockedType = typeof(myMock)
     auto container = new shared DependencyContainer;

     container.register!(SomeInterface, 
MockedType)().existingInstance(myMock);
     /* ... */
}
```

The problem with this is that register has the signature 
described above, i.e. register(T, S : T)() and that the mock 
template from unit-threaded is actually implemented by a struct 
which is "alias this"ed (how do you call that...? :D) to a class 
which is derived from the mocked interface. This means I run 
exactly into the problem I described in the first post.

Now I could ask the author of poodinis to remove the restriction 
on the template parameters for register, but it actually 
perfectly makes sense to have that restriction, because we are 
registering a concrete type as an abstract type.

I also had a quick look at the implementation of mock, which 
seems to be quite complicated already. So I fear that changing 
this implementation to using a derived class directly is unlikely 
to happen.

So I am back to my question: Why do we have this strange 
behavior? All compiler version on run.dlang.io behave like that, 
so I suppose there is some reason for this...?

[0] https://github.com/mbierlee/poodinis
[1] https://github.com/atilaneves/unit-threaded


More information about the Digitalmars-d-learn mailing list