How to move from Unique(someClass) to Unique(someInterface)?

vushu danvu.hustle at gmail.com
Sun Nov 27 17:06:31 UTC 2022


On Saturday, 16 May 2020 at 17:45:56 UTC, Konstantin wrote:
> import std.stdio;
> import automem;
> import std.experimental.allocator.mallocator : Mallocator;
>
> interface IGetInt
> {
>     @nogc int GetInt();
> }
>
> class Foo : IGetInt
> {
>     @nogc int GetInt()
>     {
>         return 42;
>     }
> }
>
> @nogc void main()
> {
>     auto foo = Unique!(Foo, Mallocator).construct;
>
>     printf("Value is %d!\n", foo.GetInt());
>
> //     Unique!(IGetInt, Mallocator) ifoo = Unique!(Foo, 
> Mallocator).construct;
> //     printf("Value is %d!\n", ifoo.GetInt());
> }
>
> How to cast from a class to an interface in such situation? Is 
> it possible?
>
> And one more question: Where can i find actual status(or plans) 
> of @nogc support for Phobos and language constructs?

I'm actually also very curious about this issue, since I come 
from c++ where this is possible, and it is a very common 
functionality for example for dependency inversion and dependency 
injection or mocking. It would be very nice to have this working.


More information about the Digitalmars-d-learn mailing list