Thread overview
Interfaces
Apr 09, 2016
alexander Patapoff
Apr 09, 2016
ag0aep6g
Apr 09, 2016
alexander Patapoff
April 09, 2016
is there a way for me to do this in D? In java, one is able to create a new instance of an interface.

interface Action
{
  void actions(T t);
}

class testAction
{
  this()
   {
    Action action = new Action()
       {
         void actions(T t){...}
       }
   }

}

I found this feature really useful in java. Is there any way I can duplicate this process, without the mixin func?
April 09, 2016
On 09.04.2016 10:45, alexander Patapoff wrote:
> is there a way for me to do this in D? In java, one is able to create a
> new instance of an interface.
>
> interface Action
> {
>    void actions(T t);
> }
>
> class testAction
> {
>    this()
>     {
>      Action action = new Action()
>         {
>           void actions(T t){...}
>         }
>     }
>
> }
>
> I found this feature really useful in java. Is there any way I can
> duplicate this process, without the mixin func?

There is a somewhat obscure feature which lets you declare and instantiate a class at the same time:

----
interface Action
{
  void actions(int x);
}

void main()
{
  Action action = new class Action {
      void actions(int x) {/* ... */}
  };
}
----

http://dlang.org/spec/class.html#anonymous
April 09, 2016
On Saturday, 9 April 2016 at 08:59:13 UTC, ag0aep6g wrote:
> On 09.04.2016 10:45, alexander Patapoff wrote:
>> [...]
>
> There is a somewhat obscure feature which lets you declare and instantiate a class at the same time:
>
> ----
> interface Action
> {
>   void actions(int x);
> }
>
> void main()
> {
>   Action action = new class Action {
>       void actions(int x) {/* ... */}
>   };
> }
> ----
>
> http://dlang.org/spec/class.html#anonymous

Yaaay! thank you!