3 ms·
Like others said, this can be done as a library type in D: http://p0nce.github.io/d-idioms/#Recursive-Sum-Type-with-matching http://p0nce.github.io/d-idioms/#Re
by p0nce 11y ago
Like others said, this can be done as a library type in D: http://p0nce.github.io/d-idioms/#Recursive-Sum-Type-with-matching http://p0nce.github.io/d-idioms/#Recursive-Sum-Type-with-mat...
edit: not that it proves anything, just that "it can eventually be done".
- tomjakubowski 11y agoCan you name the variants to distinguish two variants which "carry" the same types? It wouldn't be as useful if you can't express something like: enum ConnState { Disconnected, Connecting, Connected(net::TcpSocket), Transferring(net::TcpSocket), }
- jeremiep 11y agoI would probably do it like this in D (disclaimer: I haven't tested this code!) import std.typecons; import std.socket; import std.variant; struct Disconnected {} struct Connecting {} alias Connected = Typedef!(TcpSocket, null, "connected"); alias Transferring = Typedef!(TcpSocket, null, "transferring"); alias ConnState = Algebraic!(Disconnected, Connecting, Connected, Transferring); You could use plain structs instead of the typedef template for the same result.
- p0nce 11y agoI see what you mean. You can create such new types with std.typecons.Typedef https://dlang.org/phobos/std_typecons.html#.Typedef https://dlang.org/phobos/std_typecons.html#.Typedef Though it's less pretty.