b865017ccdc32bbaeffbf605c048a76bd915c4d1
[nit.git] / lib / 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 # Symbol classes
14 # FIXME: Should be deprecated soon
15 module symbol
16
17 redef class String
18 # Get the unique corresponding to the string
19 fun to_symbol: Symbol
20 do
21 var symbol_dictionary = once new HashMap[String, Symbol]
22 if symbol_dictionary.has_key(self) then
23 return symbol_dictionary[self]
24 else
25 var symbol = new Symbol(self)
26 symbol_dictionary[self] = symbol
27 return symbol
28 end
29 end
30 end
31
32 # A symbol is a unique immutable string
33 class Symbol
34 private var string: String
35 redef fun to_s do return string.to_s
36 end