Thread overview
How to move from Unique(someClass) to Unique(someInterface)?
May 16, 2020
Konstantin
Nov 27, 2022
vushu
Nov 28, 2022
Tejas
Nov 29, 2022
Gregor Mückl
May 16, 2020
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?
November 27, 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.
November 28, 2022

On Sunday, 27 November 2022 at 17:06:31 UTC, vushu wrote:

>

On Saturday, 16 May 2020 at 17:45:56 UTC, Konstantin wrote:

>

[...]

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.

I think it's not working here because Unique is a struct, so there's no concept of inheritance here, meanwhile that it possible in C++

November 29, 2022

On Monday, 28 November 2022 at 02:40:28 UTC, Tejas wrote:

>

On Sunday, 27 November 2022 at 17:06:31 UTC, vushu wrote:

>

On Saturday, 16 May 2020 at 17:45:56 UTC, Konstantin wrote:

>

[...]

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.

I think it's not working here because Unique is a struct, so there's no concept of inheritance here, meanwhile that it possible in C++

It should be possible to make template structs assignable/initializable from a compatible wrapped type with a custom constructor and a custom opAssign. There is a bit of fiddling with constraints involved to prohibit implicit casting of the wrapped type in ways that shouldn't work. See this incomplete proof of concept:

interface IFoo { }
class Foo : IFoo { }

struct Unique(T) {
    T value;

    // This should check that T2 is another instantiation of Unique instead of this compiles trait
    this(T2)(T2 v) if(!__traits(compiles, this.value = v.value))
    {
        this.value = v;
    }

    this(T2)(Unique!T2 other) if(__traits(compiles, this.value = other.value))
    {
        this.value = other.value;
    }

    void opAssign(T2)(Unique!T2 other) {
        value = other.value;
    }
}

void main()
{
    IFoo foo = new Foo();

    Unique!IFoo f = Unique!Foo();
    //Unique!IFoo f2 = Unique!int(); // error: assignment from incompaatible type
}

The reason why automem.Unique doesn't support this is probably hidden within the intended behavior of Unique. Maybe Adam Ruppe can shed some light on this.