From 6093bbedc0c0cf39650c4cd931afb18feb1bcda8 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Tue, 16 Jan 2007 20:33:20 +0000 Subject: enabled -Xunapply. fixed bug contrib 291. Added Map/Set types to Predef. Option no longer inherits from Iterable, but there's an implicit conversion. various other small things. --- src/library/scala/Option.scala | 20 ++++++++++++++------ src/library/scala/Predef.scala | 4 ++++ src/library/scala/collection/Set.scala | 2 +- 3 files changed, 19 insertions(+), 7 deletions(-) (limited to 'src/library') diff --git a/src/library/scala/Option.scala b/src/library/scala/Option.scala index 78f501fa9f..96d9fb2457 100644 --- a/src/library/scala/Option.scala +++ b/src/library/scala/Option.scala @@ -14,6 +14,14 @@ package scala; import Predef._ +object Option { + implicit def option2Iterable[a](xo: Option[a]): Iterable[a] = xo match { + case Some(x) => List(x) + case None => Nil + } +} + + /** This class represents optional values. Instances of Option * are either instances of case class Some or it is case * object None. @@ -22,7 +30,7 @@ import Predef._ * @author Matthias Zenger * @version 1.0, 16/07/2003 */ -sealed abstract class Option[+A] extends Iterable[A] with Product { +sealed abstract class Option[+A] extends Product { def isEmpty: Boolean = this match { case None => true @@ -42,22 +50,22 @@ sealed abstract class Option[+A] extends Iterable[A] with Product { case Some(x) => x } - override def map[B](f: A => B): Option[B] = this match { + def map[B](f: A => B): Option[B] = this match { case None => None case Some(x) => Some(f(x)) } - override def flatMap[B](f: A => Iterable[B]): Iterable[B] = this match { + def flatMap[B](f: A => Option[B]): Option[B] = this match { case None => None case Some(x) => f(x) } - override def filter(p: A => Boolean): Option[A] = this match { + def filter(p: A => Boolean): Option[A] = this match { case None => None case Some(x) => if (p(x)) Some(x) else None } - override def foreach(f: A => Unit): Unit = this match { + def foreach(f: A => Unit): Unit = this match { case None => () case Some(x) => f(x) } @@ -67,7 +75,7 @@ sealed abstract class Option[+A] extends Iterable[A] with Product { case Some(x) => Iterator.fromValues(x) } - override def toList: List[A] = this match { + def toList: List[A] = this match { case None => List() case Some(x) => List(x) } diff --git a/src/library/scala/Predef.scala b/src/library/scala/Predef.scala index 8178d1d5d0..05f38e8688 100644 --- a/src/library/scala/Predef.scala +++ b/src/library/scala/Predef.scala @@ -61,9 +61,13 @@ object Predef { type Function[-a,+b] = Function1[a,b] + type Map[a, b] = collection.mutable.Map[a, b] + type Set[a] = collection.mutable.Set[a] + val Map = collection.mutable.Map val Set = collection.mutable.Set + // errors and asserts ------------------------------------------------- def error(message: String): Nothing = throw new Error(message) diff --git a/src/library/scala/collection/Set.scala b/src/library/scala/collection/Set.scala index 66e9b3b4a4..2de995df25 100644 --- a/src/library/scala/collection/Set.scala +++ b/src/library/scala/collection/Set.scala @@ -98,6 +98,6 @@ trait Set[A] extends (A => Boolean) with Iterable[A] { * * @return a string showing all elements of this set. */ - override def toString(): String = mkString("{", ", ", "}") + override def toString(): String = mkString("Set(", ", ", ")") } -- cgit v1.2.3