by thinkpad20 · 10y ago · 0 comments
enum MyLanguage {
Var(String),
Int(i32),
Sum(Box<MyLang>, Box<MyLang>),
Let(String, Box<MyLang>, Box<MyLang>)
}
(Apologies if I got a few things wrong; I just mean the general idea)Seems like without a construct like this, you'd have to use subclasses or something similar, which (to me) isn't quite as nice.
alias MyLanguage = VariantN!(<insert types here>);
edit: not that it proves anything, just that "it can eventually be done".
enum ConnState {
Disconnected,
Connecting,
Connected(net::TcpSocket),
Transferring(net::TcpSocket),
} 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.You can create such new types with std.typecons.Typedef https://dlang.org/phobos/std_typecons.html#.Typedef
Though it's less pretty.