Created
July 3, 2012 11:55
-
-
Save alexnask/3039317 to your computer and use it in GitHub Desktop.
ooc symbol implementation
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 structs/ArrayList | |
| Symbol: class { | |
| value: String | |
| table: static ArrayList<This> | |
| new: static func(.value) -> const This { | |
| if(!table) table = ArrayList<This> new() | |
| for(sym in table) { | |
| if(sym value == value) return sym | |
| } | |
| this := This alloc() as This | |
| this value = value | |
| table add(this) | |
| this | |
| } | |
| toString: func -> String { value } | |
| toInt: func -> Int { this as Int } | |
| } |
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 Symbol | |
| Symbol new("foo") toInt() toString() println() | |
| Symbol new("foo") toInt() toString() println() | |
| // The same number should be printed both times | |
| // :foo should translate to Symbol new("foo") | |
| // Same with :"foo" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An ArrayList? Seriously? What about a HashMap instead?