summaryrefslogtreecommitdiff
path: root/src/library/scala/Symbol.scala
diff options
context:
space:
mode:
authorGeoffrey Washburn <geoffrey.washburn@epfl.ch>2008-04-07 12:15:54 +0000
committerGeoffrey Washburn <geoffrey.washburn@epfl.ch>2008-04-07 12:15:54 +0000
commit48fdb8620aeb3253f0048667044b39301c1d77c8 (patch)
tree1677e938deef28b99a0d0cd2022c34f05c906d92 /src/library/scala/Symbol.scala
parent09d3a7bb5b1a96300b78e7f1b38f135c5ab80d20 (diff)
downloadscala-48fdb8620aeb3253f0048667044b39301c1d77c8.tar.gz
scala-48fdb8620aeb3253f0048667044b39301c1d77c8.tar.bz2
scala-48fdb8620aeb3253f0048667044b39301c1d77c8.zip
New reorg plan
Diffstat (limited to 'src/library/scala/Symbol.scala')
-rw-r--r--src/library/scala/Symbol.scala69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/library/scala/Symbol.scala b/src/library/scala/Symbol.scala
new file mode 100644
index 0000000000..6f7ca958c7
--- /dev/null
+++ b/src/library/scala/Symbol.scala
@@ -0,0 +1,69 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003-2008, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+\* */
+
+// $Id$
+
+
+package scala
+
+import scala.collection.jcl
+
+private[scala] object internedSymbols extends jcl.WeakHashMap[String, ref.WeakReference[Symbol]]
+
+/** <p>
+ * This class provides a simple way to get unique objects for
+ * equal strings. Since symbols are interned, they can be compared using
+ * reference equality. Instances of
+ * <code>Symbol</code> can be created easily with Scala's built-in
+* quote mechanism.
+ * </p>
+ * <p>
+ * For instance, the <a href="http://scala-lang.org/" target="_top">Scala</a>
+ * term <code>'mysym</code> will invoke the constructor of the
+ * <code>Symbol</code> class in the following way:
+ * <code>Symbol("mysym")</code>.
+ * </p>
+ *
+ * @author Martin Odersky, Iulian Dragos
+ * @version 1.8
+ */
+@serializable
+final class Symbol private (val name: String) {
+
+ /** Converts this symbol to a string.
+ */
+ override def toString(): String = "'" + name
+
+ @throws(classOf[java.io.ObjectStreamException])
+ private def readResolve(): Any = Symbol.apply(name)
+}
+
+object Symbol {
+
+ /** <p>
+ * Makes this symbol into a unique reference.
+ * </p>
+ * <p>
+ * If two interened symbols are equal (i.e. they have the same name)
+ * then they must be identical (wrt reference equality).
+ * </p>
+ *
+ * @return the unique reference to this string.
+ */
+ def apply(name: String): Symbol = internedSymbols.synchronized {
+ internedSymbols.get(name).flatMap(_.get) match {
+ case Some(sym) => sym
+ case _ =>
+ val sym = new Symbol(name)
+ internedSymbols(name) = new ref.WeakReference(sym)
+ sym
+ }
+ }
+
+ def unapply(other: Symbol): Option[String] = Some(other.name)
+}