lib: move symbol outside standard
[nit.git] / lib / symbol.nit
diff --git a/lib/symbol.nit b/lib/symbol.nit
new file mode 100644 (file)
index 0000000..d3a27ec
--- /dev/null
@@ -0,0 +1,39 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2005-2008 Jean Privat <jean@pryen.org>
+#
+# This file is free software, which comes along with NIT.  This software is
+# distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A 
+# PARTICULAR PURPOSE.  You can modify it is you want,  provided this header
+# is kept unaltered, and a notification of the changes is added.
+# You  are  allowed  to  redistribute it and sell it, alone or is a part of
+# another product.
+
+# Symbol classes
+# FIXME: Should be deprecated soon
+package symbol
+
+redef class String
+       # Get the unique corresponding to the string
+       fun to_symbol: Symbol
+       do
+               var symbol_dictionary = once new HashMap[String, Symbol]
+               if symbol_dictionary.has_key(self) then
+                       return symbol_dictionary[self]
+               else
+                       var symbol = new Symbol(self)
+                       symbol_dictionary[self] = symbol
+                       return symbol
+               end
+       end
+end
+
+# A symbol is a unique immutable string
+class Symbol
+       var _string: String
+       redef fun to_s do return _string.to_s
+
+       # Only used by String::to_symbol
+       private init(s: String) do _string = s
+end