d953f36389d3071b7e77fd08564f5fd445ac4f1b
[nit.git] / lib / standard / symbol.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2005-2008 Jean Privat <jean@pryen.org>
4 #
5 # This file is free software, which comes along with NIT. This software is
6 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 # is kept unaltered, and a notification of the changes is added.
10 # You are allowed to redistribute it and sell it, alone or is a part of
11 # another product.
12
13 # This module introduce the Symbol class
14 package symbol
15
16 import string
17 private import hash
18
19 redef class String
20 # Get the unique corresponding to the string
21 meth to_symbol: Symbol
22 do
23 var symbol_dictionary = once new HashMap[String, Symbol]
24 if symbol_dictionary.has_key(self) then
25 return symbol_dictionary[self]
26 else
27 var symbol = new Symbol(self)
28 symbol_dictionary[self] = symbol
29 return symbol
30 end
31 end
32 end
33
34 # A symbol is a unique unmutable string
35 class Symbol
36 attr _string: String
37 redef meth to_s do return _string.to_s
38
39 # Only used by String::to_symbol
40 private init(s: String) do _string = s
41 end