How to make Rust trait like feature in dlang ?
lili
akozhao at tencent.com
Sun Jun 16 15:56:41 UTC 2019
Hi, guys:
I really like Rust trait feature. Use it can implement
duck-type simple.
base on inherited OO is complex and hard, because you need to
design inherited tree well.
and this is not easy. In real word need runtime polymorphic is
uncommon. lots of is just need compile time polymorphic.
for example: design a network socket lib you need to implement
TCP,UDP,Unix socket and these
sockets have need implement send, recv function.
interface SocketLike {
int send(ubyte[] data, uint32 len);
int recv(ubyte[] buff, uint32 len);
}
struct TcpSocket : SocketLike {...} // in dlang struct can not
allows to inherited interface
struct UdpSocket : SocketLike {...}
struct UnixSocket : SocketLike {...}
function UseSocketLike(SocketLike socket)
{
socket.recv()
....
}
I not need runtime polymorphic, just need compiler check real
argument is SocketLike type.
How do this, can dlang support conception like feature.
More information about the Digitalmars-d
mailing list