Last active
December 3, 2015 18:40
-
-
Save kubo39/533cec111d72e185a4e0 to your computer and use it in GitHub Desktop.
Operator Oveloading (compared with http://qiita.com/szktty/items/9fa3b972fbdae7aaa0ef)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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