~scompall/stylish-types/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
Messing with the new reflection API

based on 2.10.0

scala> val ju: scala.reflect.api.Universe = scala.reflect.runtime.universe
ju: scala.reflect.api.Universe = scala.reflect.runtime.JavaUniverse@2785982c

scala> ju.typeOf[Option[_]]
scala.reflect.internal.MissingRequirementError: object $iw not found.

// maybe with {type TypeTag = ...} would improve matters?

scala> val ju: scala.reflect.api.JavaUniverse = scala.reflect.runtime.universe
val ju: scala.reflect.api.JavaUniverse = scala.reflect.runtime.universe

scala> ju.typeOf[Option[_]]
res1: ju.Type = scala.Option[_]

// which is <: Universe.TypeApi

scala> ju.typeOf[Option[Int]]
res2: ju.Type = scala.Option[Int]

scala> ju.typeOf[Option[Int]].takesTypeArgs
res7: Boolean = false

scala> ju.typeOf[Option[Int]].typeConstructor
res8: ju.Type = scala.Option

scala> ju.typeOf[Option[Int]].typeConstructor.takesTypeArgs
res9: Boolean = true

scala> ju.typeOf[Option[Int] {def blah: Int}].typeConstructor
res11: ju.Type = scala.Option{def blah: Int}

scala> ju.typeOf[Option[Int] {def blah: Int}].declarations
res14: ju.MemberScope = SynchronizedOps(method blah)

scala> ju.typeOf[Option[Int]].declarations
res15: ju.MemberScope = SynchronizedOps(constructor Option, method isEmpty, method isDefined, method get, method getOrElse, method orNull, method map, method fold, method flatMap, method flatten, method filter, method filterNot, method nonEmpty, method withFilter, class WithFilter, method exists, method forall, method foreach, method collect, method orElse, method iterator, method toList, method toRight, method toLeft)

/* not sure where this has gone
scala> ju.typeOf[Option[Int]].isStructuralRefinement
res5: Boolean = false
*/

// alas, rootMirror doesn't get the current thread's classloader's
// mirror, so we still need runtimeMirror from wherever that comes
// from
scala> val mir = ju.rootMirror
mir: ju.Mirror = JavaMirror with java.net.URLClassLoader@32fd4662 of type class...

scala> mir staticPackage "scala"
res0: ju.ModuleSymbol = package scala

scala> (mir staticPackage "scala").isModule
res2: Boolean = true

scala> (mir staticPackage "scala").isTerm
res3: Boolean = true

scala> (mir staticClass "scala.Option")
res6: ju.ClassSymbol = class Option

scala> (mir staticClass "scala.Option").asType.toType.members
(mir staticClass "scala.Option").asType.toType.members
res12: ju.MemberScope = Scopes(method toLeft, method toRight, method toList, method iterator, method orElse...

scala> (mir staticClass "scala.Option").knownDirectSubclasses
res17: Set[ju.Symbol] = Set(object None, class Some)

scala> val balh = (mir staticClass "scala.Option").asType
balh: ju.TypeSymbol = class Option

scala> balh.toType
res20: ju.Type = Option[A]

scala> balh.toType.typeConstructor
res22: ju.Type = Option

scala> balh.typeParams
res23: List[ju.Symbol] = List(type A)

scala> balh.typeSignature
res24: ju.Type = 
[+A]AnyRef
        with Product
        with Serializable {
  def <init>(): Option[A]
  def isEmpty: Boolean
  def isDefined: Boolean
  def get: A
  final def getOrElse[B >: A](default: => B): B
  final def orNull[A1 >: A](implicit ev: <:<[Null,A1]): A1
  final def map[B](f: A => B): Option[B]
...