Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What other languages support Go's style of interfacing without explicit declaration?

I am a reasonably experiences hobby programmer, and I have good familiarity with C++, D, Java, C# and others.

With the exception of Go, almost every language requires me to explicitly state that I am implementing an interface. This is borderline ridiculous, since we today have compilers for languages like Haskell, which can do almost full-program type inference with very few hints.

What I am looking for is a programming language that does this:

interface ITest {
    void Test();
}

class Test {
    void Test() { }
}

void main() {
    ITest x;
    x = new Test;
}

What languages would see this, and automatically flag Test as implementing ITest?

ETA: I am not looking for duck typing. I am looking for strictly typed languages with inference.

like image 537
Kile Damgaard Asmussen Avatar asked Jul 24 '13 16:07

Kile Damgaard Asmussen


1 Answers

D has something called wrap in its standard library, Phobos, which can do what you're looking for. Here's an example copied from the function's unittest:

interface A {
  int run();
}

interface B {
  int stop();
  @property int status();
}

class X {
  int run() {
    return 1;
  }

  int stop() {
    return 2;
  }

  @property int status() {
    return 3;
  }
}

auto x = new X();

auto ab = x.wrap!(A, B);
A a = ab;
B b = ab;
assert(a.run() == 1);
assert(b.stop() == 2);
assert(b.status == 3);

The work will be available starting from v2.064, and it is already on git HEAD.

like image 153
yaz Avatar answered Oct 29 '22 18:10

yaz