Skip to content

Instantly share code, notes, and snippets.

@run-dlang
Created January 14, 2019 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save run-dlang/a8fbbc5dc49346b887b9aaee8758ffca to your computer and use it in GitHub Desktop.
Save run-dlang/a8fbbc5dc49346b887b9aaee8758ffca to your computer and use it in GitHub Desktop.
Code shared from run.dlang.io.
import std.stdio;
import std.variant : Variant;
struct var {
private Variant[string] values;
@property
Variant opDispatch(string name)() const {
return values[name];
}
@property
void opDispatch(string name, T)(T val) {
values[name] = val;
}
}
void main()
{
var test;
// Normal flow
test.foo = "test";
test.bar = 50;
writeln("test.foo = ", test.foo);
writeln("test.bar = ", test.bar);
// Error flow
with (test) {
foobar = 3.14;
}
writeln(test.foobar);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment