Thread overview
Question about interface implementation
May 21, 2023
Theo
May 21, 2023
ag0aep6g
May 21, 2023
ag0aep6g
May 21, 2023
Theo
May 21, 2023
ag0aep6g
May 21, 2023
Theo
May 21, 2023
see comments in the code below. they are my questions.

But feel free to ignore the comment about 'private(this)' ;-)


interface Ship
{
    @safe void setSpeed(int speed);
    @safe int getSpeed();
}

class PirateShip : Ship
{
    private int speed = 0; // If only I had 'private(this)' !!

    // how do I know this method is actually an implementation of an interface method
    // and not a method specific to this class?
    public void setSpeed(int speed)
    {
        this.speed = speed;
    }

   // how do I know this method is actually an implementation of an interface method
   // and not a method specific to this class?
   public int getSpeed()
    {
        return speed;
    }
}


class MerchantShip : Ship
{
    private int speed = 0; // If only I had 'private(this)' !!

    // how do I know this method is actually an implementation of an interface method
    // and not a method specific to this class?
    // AND ... how come I can change a @safe interface method into a @trusted one?
    public @trusted void setSpeed(int speed)
    {
        int *s = void; // Mmmm.. and my interface all had @safe methods!!
        this.speed = speed;
    }

    // how do I know this method is actually an implementation of an interface method
    // and not a method specific to this class?
    // AND ... how come I can change a @safe interface method into a @trusted one?
    public @trusted int getSpeed()
    {
        int *s = void; // Mmmm.. and my interface all had @safe methods!!
        return speed;
    }

}

May 21, 2023
On 21.05.23 11:55, Theo wrote:
> class MerchantShip : Ship
> {
>      private int speed = 0; // If only I had 'private(this)' !!
> 
>      // how do I know this method is actually an implementation of an interface method
>      // and not a method specific to this class?
>      // AND ... how come I can change a @safe interface method into a @trusted one?
>      public @trusted void setSpeed(int speed)
>      {
>          int *s = void; // Mmmm.. and my interface all had @safe methods!!
>          this.speed = speed;
>      }
[...]
> }

As far as I understand, a method that has the right signature is always an implementation of the interface. There is no way to make a method of the same name, with the same parameters, etc. that is "specific to the class".

@trusted means that you're allowed to use @system features in the implementation while the function must follow the restrictions of @safe when called. Since @trusted functions are guaranteed (by the programmer) to be safe, they are allowed to overload/implement @safe functions/prototypes.

If you create an @trusted function that is not safe to call, that's an error on your part.
May 21, 2023
On 21.05.23 12:28, ag0aep6g wrote:
> Since @trusted functions are guaranteed (by the programmer) to be safe, they are allowed to overload/implement @safe functions/prototypes.

*override
May 21, 2023
On Sunday, 21 May 2023 at 10:33:07 UTC, ag0aep6g wrote:
> On 21.05.23 12:28, ag0aep6g wrote:
>> Since @trusted functions are guaranteed (by the programmer) to be safe, they are allowed to overload/implement @safe functions/prototypes.
>
> *override

oh ok. so i can override a @safe interface method with a @trusted implementation of that method, and nobody will ever know. I'm not sure how i feel about that.

Likely I would never put @safe in the interface anyway, but even so...

As for the other part, if I use an abstract base class, I *must* indicate when i'm overriding the base class method by explicately saying 'override'.

Would be nice to have something for an interface method, so when it's being implemented, you know that method is the implementation of an interface method, and not just some method specific to that class - see my idea below.

interface Ship
{
    void setSpeed(int speed);
    int getSpeed();
}

class PirateShip : Ship
{
    private int speed = 0; // Here, I really mean 'private(this)'

    public void setSpeed(int speed) : implements Ship
    {
        this.speed = speed;
    }

    public int getSpeed() : implements Ship
    {
        return speed;
    }
}

// other ships.....

May 21, 2023
On 21.05.23 12:55, Theo wrote:
> As for the other part, if I use an abstract base class, I *must* indicate when i'm overriding the base class method by explicately saying 'override'.

I wouldn't mind if implementing interface methods required `override` as well. I don't know if there is a rationale for the inconsistency.

May 21, 2023
On Sunday, 21 May 2023 at 11:20:30 UTC, ag0aep6g wrote:
> On 21.05.23 12:55, Theo wrote:
>> As for the other part, if I use an abstract base class, I *must* indicate when i'm overriding the base class method by explicately saying 'override'.
>
> I wouldn't mind if implementing interface methods required `override` as well. I don't know if there is a rationale for the inconsistency.

Consistency, which can also aid in self-documenting, might look something like this:

----- using interface -----

interface Ship
{
    void setSpeed(int speed); // must 'implement'
    int getSpeed(); // must 'implement'
}

class PirateShip : Ship
{
    private(this) int speed = 0;

    public void setSpeed(int speed) : implements Ship
    {
        this.speed = speed;
    }

    public int getSpeed() : implements Ship
    {
        return speed;
    }
}

----- using base class -----
abstract base class Ship
{
    abstract void setSpeed(int speed); // must 'implement'
    abstract int getSpeed(); // must 'implement'
    void someotherMethod(); // can 'override'.
}

class PirateShip : Ship
{
    private(this) int speed = 0;

    public void setSpeed(int speed) : implements Ship
    {
        this.speed = speed;
    }

    public int getSpeed() : implements Ship
    {
        return speed;
    }

    public void someotherMethod() : overrides Ship
    {

    }
}

-----------------