diff options
author | Denys Shabalin <denys.shabalin@typesafe.com> | 2014-01-22 13:43:50 +0100 |
---|---|---|
committer | Denys Shabalin <denys.shabalin@typesafe.com> | 2014-01-22 14:06:30 +0100 |
commit | ca74550416049f106e4770edba70be918d674d7f (patch) | |
tree | 27785d4d1bbb547fd0f46f1565f7e37e3ff4cc8b /src/reflect | |
parent | 115cd16aca35c8b4000b86f4affd4df243202fd2 (diff) | |
download | scala-ca74550416049f106e4770edba70be918d674d7f.tar.gz scala-ca74550416049f106e4770edba70be918d674d7f.tar.bz2 scala-ca74550416049f106e4770edba70be918d674d7f.zip |
Expose seq field for variable arity definitions
In 2.11 we've changed TupleClass, ProductClass and FunctionClass
endpoints to be exposed as (Int => Symbol) functions that never throw
exceptions but rather return NoSymbol instead of previous error-prone
indexed access on array that could explode.
While simplifying one use case (indexed access) it complicated
ability to check if symbol at hand is in fact a tuple, product or
function:
(1 to 22).map(TupleClass).toList.contains(symbol)
To cover this extra use case we add a seq method to the variable arity
class definitions that exposes a corresponding sequence of class symbols:
TupleClass.seq.contains(symbol)
Diffstat (limited to 'src/reflect')
-rw-r--r-- | src/reflect/scala/reflect/api/StandardDefinitions.scala | 18 | ||||
-rw-r--r-- | src/reflect/scala/reflect/internal/Definitions.scala | 2 |
2 files changed, 14 insertions, 6 deletions
diff --git a/src/reflect/scala/reflect/api/StandardDefinitions.scala b/src/reflect/scala/reflect/api/StandardDefinitions.scala index e255d305f7..1a8885e6b5 100644 --- a/src/reflect/scala/reflect/api/StandardDefinitions.scala +++ b/src/reflect/scala/reflect/api/StandardDefinitions.scala @@ -214,6 +214,14 @@ trait StandardDefinitions { /** The module symbol of module `scala.Some`. */ def SomeModule: ModuleSymbol + /** Function-like api that lets you acess symbol + * of the definition with given arity and also look + * through all known symbols via `seq`. + */ + abstract class VarArityClassApi extends (Int => Symbol) { + def seq: Seq[ClassSymbol] + } + /** Function-like object that maps arity to symbols for classes `scala.ProductX`. * - 0th element is `Unit` * - 1st element is `Product1` @@ -222,7 +230,7 @@ trait StandardDefinitions { * - 23nd element is `NoSymbol` * - ... */ - def ProductClass: Int => Symbol + def ProductClass: VarArityClassApi /** Function-like object that maps arity to symbols for classes `scala.FunctionX`. * - 0th element is `Function0` @@ -232,17 +240,17 @@ trait StandardDefinitions { * - 23nd element is `NoSymbol` * - ... */ - def FunctionClass: Int => Symbol + def FunctionClass: VarArityClassApi /** Function-like object that maps arity to symbols for classes `scala.TupleX`. * - 0th element is `NoSymbol` - * - 1st element is `Product1` + * - 1st element is `Tuple1` * - ... - * - 22nd element is `Product22` + * - 22nd element is `Tuple22` * - 23nd element is `NoSymbol` * - ... */ - def TupleClass: Int => Symbol + def TupleClass: VarArityClassApi /** Contains Scala primitive value classes: * - Byte diff --git a/src/reflect/scala/reflect/internal/Definitions.scala b/src/reflect/scala/reflect/internal/Definitions.scala index c2939e69b5..5b06239863 100644 --- a/src/reflect/scala/reflect/internal/Definitions.scala +++ b/src/reflect/scala/reflect/internal/Definitions.scala @@ -522,7 +522,7 @@ trait Definitions extends api.StandardDefinitions { def hasJavaMainMethod(sym: Symbol): Boolean = (sym.tpe member nme.main).alternatives exists isJavaMainMethod - class VarArityClass(name: String, maxArity: Int, countFrom: Int = 0, init: Option[ClassSymbol] = None) extends (Int => Symbol) { + class VarArityClass(name: String, maxArity: Int, countFrom: Int = 0, init: Option[ClassSymbol] = None) extends VarArityClassApi { private val offset = countFrom - init.size private def isDefinedAt(i: Int) = i < seq.length + offset && i >= offset val seq: IndexedSeq[ClassSymbol] = (init ++: countFrom.to(maxArity).map { i => getRequiredClass("scala." + name + i) }).toVector |