Skip to content

Instantly share code, notes, and snippets.

@kubo39
Last active December 3, 2015 18:40
Show Gist options
  • Select an option

  • Save kubo39/533cec111d72e185a4e0 to your computer and use it in GitHub Desktop.

Select an option

Save kubo39/533cec111d72e185a4e0 to your computer and use it in GitHub Desktop.
Operator Oveloading (compared with http://qiita.com/szktty/items/9fa3b972fbdae7aaa0ef)
import std.stdio : writeln;
class Company
{
Employee[] employees;
auto opOpAssign(string op)(Employee employee) if (op == "~")
{
employees ~= employee;
}
string opIndex(ulong id)
{
foreach (e; employees) if (e.id == id) return e.name;
throw new Exception("Not found entry.");
}
void opIndexAssign(string name, ulong id)
{
foreach (ref e; employees) {
if (e.id == id) {
e.name = name;
return;
}
}
throw new Exception("Not found entry.");
}
}
class Employee
{
ulong id;
string name;
this(typeof(this.tupleof) xs)
{
this.tupleof = xs;
}
}
version(unittest){}
else
void main()
{
auto company = new Company;
foreach (i, name; ["kubo38", "kubo39", "kubo40"]) company ~= new Employee(i, name);
company[1].writeln;
company[1] = "unko";
company[1].writeln;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment