Skip to content

Commit e9d4598

Browse files
committed
Merge pull request scala#3398 from densh/topic/var-arity-class-seq
Expose seq field for variable arity definitions
2 parents 115cd16 + ca74550 commit e9d4598

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

src/reflect/scala/reflect/api/StandardDefinitions.scala

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,14 @@ trait StandardDefinitions {
214214
/** The module symbol of module `scala.Some`. */
215215
def SomeModule: ModuleSymbol
216216

217+
/** Function-like api that lets you acess symbol
218+
* of the definition with given arity and also look
219+
* through all known symbols via `seq`.
220+
*/
221+
abstract class VarArityClassApi extends (Int => Symbol) {
222+
def seq: Seq[ClassSymbol]
223+
}
224+
217225
/** Function-like object that maps arity to symbols for classes `scala.ProductX`.
218226
* - 0th element is `Unit`
219227
* - 1st element is `Product1`
@@ -222,7 +230,7 @@ trait StandardDefinitions {
222230
* - 23nd element is `NoSymbol`
223231
* - ...
224232
*/
225-
def ProductClass: Int => Symbol
233+
def ProductClass: VarArityClassApi
226234

227235
/** Function-like object that maps arity to symbols for classes `scala.FunctionX`.
228236
* - 0th element is `Function0`
@@ -232,17 +240,17 @@ trait StandardDefinitions {
232240
* - 23nd element is `NoSymbol`
233241
* - ...
234242
*/
235-
def FunctionClass: Int => Symbol
243+
def FunctionClass: VarArityClassApi
236244

237245
/** Function-like object that maps arity to symbols for classes `scala.TupleX`.
238246
* - 0th element is `NoSymbol`
239-
* - 1st element is `Product1`
247+
* - 1st element is `Tuple1`
240248
* - ...
241-
* - 22nd element is `Product22`
249+
* - 22nd element is `Tuple22`
242250
* - 23nd element is `NoSymbol`
243251
* - ...
244252
*/
245-
def TupleClass: Int => Symbol
253+
def TupleClass: VarArityClassApi
246254

247255
/** Contains Scala primitive value classes:
248256
* - Byte

src/reflect/scala/reflect/internal/Definitions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ trait Definitions extends api.StandardDefinitions {
522522
def hasJavaMainMethod(sym: Symbol): Boolean =
523523
(sym.tpe member nme.main).alternatives exists isJavaMainMethod
524524

525-
class VarArityClass(name: String, maxArity: Int, countFrom: Int = 0, init: Option[ClassSymbol] = None) extends (Int => Symbol) {
525+
class VarArityClass(name: String, maxArity: Int, countFrom: Int = 0, init: Option[ClassSymbol] = None) extends VarArityClassApi {
526526
private val offset = countFrom - init.size
527527
private def isDefinedAt(i: Int) = i < seq.length + offset && i >= offset
528528
val seq: IndexedSeq[ClassSymbol] = (init ++: countFrom.to(maxArity).map { i => getRequiredClass("scala." + name + i) }).toVector
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import scala.reflect.runtime.universe._, definitions._
2+
object Test extends App {
3+
// Tuples
4+
assert(TupleClass.seq.size == 22)
5+
assert(TupleClass(0) == NoSymbol)
6+
assert(TupleClass(23) == NoSymbol)
7+
assert((1 to 22).forall { i => TupleClass(i).name.toString == s"Tuple$i" })
8+
// Functions
9+
assert(FunctionClass.seq.size == 23)
10+
assert(FunctionClass(-1) == NoSymbol)
11+
assert(FunctionClass(23) == NoSymbol)
12+
assert((0 to 22).forall { i => FunctionClass(i).name.toString == s"Function$i" })
13+
// Products
14+
assert(ProductClass.seq.size == 23)
15+
assert(ProductClass(-1) == NoSymbol)
16+
assert(ProductClass(0) == UnitClass)
17+
assert(ProductClass(23) == NoSymbol)
18+
assert((1 to 22).forall { i => ProductClass(i).name.toString == s"Product$i" })
19+
}

0 commit comments

Comments
 (0)