From 0eb83b8db527068ef99f5068c2d380fb1827c227 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Wed, 31 Dec 2014 21:15:46 +0300 Subject: [PATCH] turns all reflect-related qualified names into fully-qualified names --- ja/overviews/macros/quasiquotes.md | 12 ++-- .../reflection/annotations-names-scopes.md | 18 ++--- .../environment-universes-mirrors.md | 36 +++++----- ja/overviews/reflection/overview.md | 22 +++--- .../reflection/symbols-trees-types.md | 70 +++++++++--------- overviews/quasiquotes/hygiene.md | 4 +- overviews/quasiquotes/lifting.md | 6 +- overviews/quasiquotes/setup.md | 6 +- .../reflection/annotations-names-scopes.md | 14 ++-- .../environment-universes-mirrors.md | 36 +++++----- overviews/reflection/overview.md | 20 +++--- overviews/reflection/symbols-trees-types.md | 72 +++++++++---------- 12 files changed, 158 insertions(+), 158 deletions(-) diff --git a/ja/overviews/macros/quasiquotes.md b/ja/overviews/macros/quasiquotes.md index e2d3d029e9..898877c298 100644 --- a/ja/overviews/macros/quasiquotes.md +++ b/ja/overviews/macros/quasiquotes.md @@ -79,22 +79,22 @@ title: 準クォート 普通の文字列補間子と違い、準クォートは単独の構文木、構文木のリスト、構文木のリストのリストの挿入または抽出を区別するために複数のスプライシングの方法をサポートしている。スプライス対象とスプライス演算子の基数のミスマッチはコンパイル時のエラーとなる。 scala> val name = TypeName("C") - name: reflect.runtime.universe.TypeName = C + name: scala.reflect.runtime.universe.TypeName = C scala> val q"class $name1" = q"class $name" - name1: reflect.runtime.universe.Name = C + name1: scala.reflect.runtime.universe.Name = C scala> val args = List(Literal(Constant(2))) - args: List[reflect.runtime.universe.Literal] = List(2) + args: List[scala.reflect.runtime.universe.Literal] = List(2) scala> val q"foo(..$args1)" = q"foo(..$args)" - args1: List[reflect.runtime.universe.Tree] = List(2) + args1: List[scala.reflect.runtime.universe.Tree] = List(2) scala> val argss = List(List(Literal(Constant(2))), List(Literal(Constant(3)))) - argss: List[List[reflect.runtime.universe.Literal]] = List(List(2), List(3)) + argss: List[List[scala.reflect.runtime.universe.Literal]] = List(List(2), List(3)) scala> val q"foo(...$argss1)" = q"foo(...$argss)" - argss1: List[List[reflect.runtime.universe.Tree]] = List(List(2), List(3)) + argss1: List[List[scala.reflect.runtime.universe.Tree]] = List(List(2), List(3)) ## コツとトリック diff --git a/ja/overviews/reflection/annotations-names-scopes.md b/ja/overviews/reflection/annotations-names-scopes.md index 7fcc78e1d8..3e949bffda 100644 --- a/ja/overviews/reflection/annotations-names-scopes.md +++ b/ja/overviews/reflection/annotations-names-scopes.md @@ -17,7 +17,7 @@ title: アノテーション、名前、スコープ、その他 Scala において宣言は `scala.annotation.Annotation` のサブタイプを用いて注釈を付けることができる。 さらに、Scala は [Java のアノテーションシステム](http://docs.oracle.com/javase/7/docs/technotes/guides/language/annotations.html#_top)に統合するため、標準 Java コンパイラによって生成されたアノテーションを取り扱うこともできる。 -アノテーションは、それが永続化されていればリフレクションを使ってインスペクトすることができるため、アノテーション付きの宣言を含むクラスファイルから読み込むことができる。カスタムアノテーション型は +アノテーションは、それが永続化されていればリフレクションを使ってインスペクトすることができるため、アノテーション付きの宣言を含むクラスファイルから読み込むことができる。カスタムアノテーション型は `scala.annotation.StaticAnnotation` か `scala.annotation.ClassfileAnnotation` を継承することで永続化することができる。 その結果、アノテーション型のインスタンスはクラスファイル内の特別な属性として保存される。 @@ -40,7 +40,7 @@ Scala または Java アノテーションに対しては `scalaArgs` は空で アノテーションの場合は、引数は `scalaArgs` に保持され、`javaArgs` は空となる。 `scalaArgs` 内の引数は型付けされた構文木として表される。 -これらの構文木はタイプチェッカより後のどのフェーズにおいても変換されないことに注意する必要がある。 +これらの構文木はタイプチェッカより後のどのフェーズにおいても変換されないことに注意する必要がある。 `javaArgs` 内の引数は `scala.reflect.api.Names#Name` から `scala.reflect.api.Annotations#JavaArgument` へのマップとして表現される。 `JavaArgument` のインスタンスは Java アノテーションの引数の様々な型を表現する: @@ -64,7 +64,7 @@ Scala または Java アノテーションに対しては `scalaArgs` は空で import scala.reflect.runtime.universe._ scala> val mapName = newTermName("map") - mapName: reflect.runtime.universe.TermName = map + mapName: scala.reflect.runtime.universe.TermName = map 上のコードでは、実行時リフレクション・ユニバースに関連付けられた `Name` を作成している。 (これはパス依存型である `reflect.runtime.universe.TermName` が表示されていることからも分かる。) @@ -72,16 +72,16 @@ Scala または Java アノテーションに対しては `scalaArgs` は空で 名前は型のメンバの照会に用いられる。例えば、`List` クラス内で宣言されている (項である) `map` メソッドを検索するには以下のようにする: scala> val listTpe = typeOf[List[Int]] - listTpe: reflect.runtime.universe.Type = scala.List[Int] + listTpe: scala.reflect.runtime.universe.Type = scala.List[Int] scala> listTpe.member(mapName) - res1: reflect.runtime.universe.Symbol = method map + res1: scala.reflect.runtime.universe.Symbol = method map 型メンバを検索するには `newTypeName` を代わりに使って `member` を呼び出す。 暗黙の変換を使って文字列から項もしくは型の名前に変換することもできる: scala> listTpe.member("map": TermName) - res2: reflect.runtime.universe.Symbol = method map + res2: scala.reflect.runtime.universe.Symbol = method map ### 標準名 @@ -115,7 +115,7 @@ Scala のプログラムにおいて、「`_root_`」のような特定の名前 以下に `List` クラスでオーバーライドされている全てのシンボルのリストを宣言順に返す具体例をみてみよう: scala> val overridden = listTpe.declarations.sorted.filter(_.isOverride) - overridden: List[reflect.runtime.universe.Symbol] = List(method companion, method ++, method +:, method toList, method take, method drop, method slice, method takeRight, method splitAt, method takeWhile, method dropWhile, method span, method reverse, method stringPrefix, method toStream, method foreach) + overridden: List[scala.reflect.runtime.universe.Symbol] = List(method companion, method ++, method +:, method toList, method take, method drop, method slice, method takeRight, method splitAt, method takeWhile, method dropWhile, method span, method reverse, method stringPrefix, method toStream, method foreach) ## Expr @@ -278,7 +278,7 @@ Java の列挙要素への参照はシンボル (`scala.reflect.api.Symbols#Symb import scala.reflect.runtime.universe._ scala> def tree = reify { final class C { def x = 2 } }.tree - tree: reflect.runtime.universe.Tree + tree: scala.reflect.runtime.universe.Tree scala> show(tree) res0: String = @@ -351,7 +351,7 @@ Java の列挙要素への参照はシンボル (`scala.reflect.api.Symbols#Symb import scala.reflect.runtime.universe._ scala> def tpe = typeOf[{ def x: Int; val y: List[Int] }] - tpe: reflect.runtime.universe.Type + tpe: scala.reflect.runtime.universe.Type scala> show(tpe) res0: String = scala.AnyRef{def x: Int; val y: scala.List[Int]} diff --git a/ja/overviews/reflection/environment-universes-mirrors.md b/ja/overviews/reflection/environment-universes-mirrors.md index 139cb6fdf8..45bcfaf81a 100644 --- a/ja/overviews/reflection/environment-universes-mirrors.md +++ b/ja/overviews/reflection/environment-universes-mirrors.md @@ -74,7 +74,7 @@ invoker ミラーを作成することができる。 ru: scala.reflect.api.JavaUniverse = ... scala> val m = ru.runtimeMirror(getClass.getClassLoader) - m: reflect.runtime.universe.Mirror = JavaMirror ... + m: scala.reflect.runtime.universe.Mirror = JavaMirror ... `InstanceMirror` はメソッド、フィールド、内部クラス、および内部オブジェクトの invoker ミラーを作成するのに使われる。作り方: `val im = m.reflect()`。 具体例: @@ -83,16 +83,16 @@ invoker ミラーを作成することができる。 defined class C scala> val im = m.reflect(new C) - im: reflect.runtime.universe.InstanceMirror = instance mirror for C@3442299e + im: scala.reflect.runtime.universe.InstanceMirror = instance mirror for C@3442299e `MethodMirror` はインスタンス・メソッド (Scala にはインスタンス・メソッドのみがある。オブジェクトのメソッドは `ModuleMirror.instance` から取得されるオブジェクト・インスタンスのインスタンス・メソッドだ。) の呼び出しに使われる。作り方: `val mm = im.reflectMethod()`。 具体例: scala> val methodX = ru.typeOf[C].declaration(ru.newTermName("x")).asMethod - methodX: reflect.runtime.universe.MethodSymbol = method x + methodX: scala.reflect.runtime.universe.MethodSymbol = method x scala> val mm = im.reflectMethod(methodX) - mm: reflect.runtime.universe.MethodMirror = method mirror for C.x: scala.Int (bound to C@3442299e) + mm: scala.reflect.runtime.universe.MethodMirror = method mirror for C.x: scala.Int (bound to C@3442299e) scala> mm() res0: Any = 2 @@ -104,16 +104,16 @@ invoker ミラーを作成することができる。 defined class C scala> val m = ru.runtimeMirror(getClass.getClassLoader) - m: reflect.runtime.universe.Mirror = JavaMirror ... + m: scala.reflect.runtime.universe.Mirror = JavaMirror ... scala> val im = m.reflect(new C) - im: reflect.runtime.universe.InstanceMirror = instance mirror for C@5f0c8ac1 + im: scala.reflect.runtime.universe.InstanceMirror = instance mirror for C@5f0c8ac1 scala> val fieldX = ru.typeOf[C].declaration(ru.newTermName("x")).asTerm.accessed.asTerm - fieldX: reflect.runtime.universe.TermSymbol = value x + fieldX: scala.reflect.runtime.universe.TermSymbol = value x scala> val fmX = im.reflectField(fieldX) - fmX: reflect.runtime.universe.FieldMirror = field mirror for C.x (bound to C@5f0c8ac1) + fmX: scala.reflect.runtime.universe.FieldMirror = field mirror for C.x (bound to C@5f0c8ac1) scala> fmX.get res0: Any = 2 @@ -121,10 +121,10 @@ invoker ミラーを作成することができる。 scala> fmX.set(3) scala> val fieldY = ru.typeOf[C].declaration(ru.newTermName("y")).asTerm.accessed.asTerm - fieldY: reflect.runtime.universe.TermSymbol = variable y + fieldY: scala.reflect.runtime.universe.TermSymbol = variable y scala> val fmY = im.reflectField(fieldY) - fmY: reflect.runtime.universe.FieldMirror = field mirror for C.y (bound to C@5f0c8ac1) + fmY: scala.reflect.runtime.universe.FieldMirror = field mirror for C.y (bound to C@5f0c8ac1) scala> fmY.get res1: Any = 3 @@ -141,19 +141,19 @@ invoker ミラーを作成することができる。 defined class C scala> val m = ru.runtimeMirror(getClass.getClassLoader) - m: reflect.runtime.universe.Mirror = JavaMirror ... + m: scala.reflect.runtime.universe.Mirror = JavaMirror ... scala> val classC = ru.typeOf[C].typeSymbol.asClass - classC: reflect.runtime.universe.Symbol = class C + classC: scala.reflect.runtime.universe.Symbol = class C scala> val cm = m.reflectClass(classC) - cm: reflect.runtime.universe.ClassMirror = class mirror for C (bound to null) + cm: scala.reflect.runtime.universe.ClassMirror = class mirror for C (bound to null) scala> val ctorC = ru.typeOf[C].declaration(ru.nme.CONSTRUCTOR).asMethod - ctorC: reflect.runtime.universe.MethodSymbol = constructor C + ctorC: scala.reflect.runtime.universe.MethodSymbol = constructor C scala> val ctorm = cm.reflectConstructor(ctorC) - ctorm: reflect.runtime.universe.MethodMirror = constructor mirror for C.(x: scala.Int): C (bound to null) + ctorm: scala.reflect.runtime.universe.MethodMirror = constructor mirror for C.(x: scala.Int): C (bound to null) scala> ctorm(2) res0: Any = C(2) @@ -165,13 +165,13 @@ invoker ミラーを作成することができる。 defined module C scala> val m = ru.runtimeMirror(getClass.getClassLoader) - m: reflect.runtime.universe.Mirror = JavaMirror ... + m: scala.reflect.runtime.universe.Mirror = JavaMirror ... scala> val objectC = ru.typeOf[C.type].termSymbol.asModule - objectC: reflect.runtime.universe.ModuleSymbol = object C + objectC: scala.reflect.runtime.universe.ModuleSymbol = object C scala> val mm = m.reflectModule(objectC) - mm: reflect.runtime.universe.ModuleMirror = module mirror for C (bound to null) + mm: scala.reflect.runtime.universe.ModuleMirror = module mirror for C (bound to null) scala> val obj = mm.instance obj: Any = C$@1005ec04 diff --git a/ja/overviews/reflection/overview.md b/ja/overviews/reflection/overview.md index 50407f78ba..c0b6137c54 100644 --- a/ja/overviews/reflection/overview.md +++ b/ja/overviews/reflection/overview.md @@ -100,27 +100,27 @@ Scala コンパイラが持つ型情報を全ては入手できない可能性 defined class Person scala> val m = ru.runtimeMirror(getClass.getClassLoader) - m: reflect.runtime.universe.Mirror = JavaMirror with ... + m: scala.reflect.runtime.universe.Mirror = JavaMirror with ... -最初のステップとして現在のクラスローダで読み込まれた (`Person` クラスを含む) +最初のステップとして現在のクラスローダで読み込まれた (`Person` クラスを含む) 全てのクラスや型をアクセス可能とするミラー `m` を取得する。 scala> val classPerson = ru.typeOf[Person].typeSymbol.asClass - classPerson: reflect.runtime.universe.ClassSymbol = class Person + classPerson: scala.reflect.runtime.universe.ClassSymbol = class Person scala> val cm = m.reflectClass(classPerson) - cm: reflect.runtime.universe.ClassMirror = class mirror for Person (bound to null) + cm: scala.reflect.runtime.universe.ClassMirror = class mirror for Person (bound to null) 次に、`reflectClass` メソッドを使って `Person` クラスの `ClassMirror` を取得する。 `ClassMirror` は `Person` クラスのコンストラクタへのアクセスを提供する。 scala> val ctor = ru.typeOf[Person].declaration(ru.nme.CONSTRUCTOR).asMethod - ctor: reflect.runtime.universe.MethodSymbol = constructor Person + ctor: scala.reflect.runtime.universe.MethodSymbol = constructor Person `Person` のコンストラクタのシンボルは実行時ユニバース `ru` を用いて `Person` 型の宣言から照会することによってのみ得られる。 scala> val ctorm = cm.reflectConstructor(ctor) - ctorm: reflect.runtime.universe.MethodMirror = constructor mirror for Person.(name: String): Person (bound to null) + ctorm: scala.reflect.runtime.universe.MethodMirror = constructor mirror for Person.(name: String): Person (bound to null) scala> val p = ctorm("Mike") p: Any = Person(Mike) @@ -143,22 +143,22 @@ Scala コンパイラが持つ型情報を全ては入手できない可能性 import scala.reflect.runtime.{universe=>ru} scala> val m = ru.runtimeMirror(p.getClass.getClassLoader) - m: reflect.runtime.universe.Mirror = JavaMirror with ... + m: scala.reflect.runtime.universe.Mirror = JavaMirror with ... `shipped` メンバにアクセスするには、前の例と同じく、`p` のクラス (`Purchase`) を含むクラスローダが読み込んだ全てのクラスを入手可能とするミラー `m` を取得することから始める。 scala> val shippingTermSymb = ru.typeOf[Purchase].declaration(ru.newTermName("shipped")).asTerm - shippingTermSymb: reflect.runtime.universe.TermSymbol = method shipped + shippingTermSymb: scala.reflect.runtime.universe.TermSymbol = method shipped 次に、`shipped` フィールドの宣言を照会して `TermSymbol` (`Symbol` 型の 1つ) を得る。 この `Symbol` は後で (何からのオブジェクトの) このフィールドの値にアクセスするのに必要なミラーを得るのに使う。 scala> val im = m.reflect(p) - im: reflect.runtime.universe.InstanceMirror = instance mirror for Purchase(Jeff Lebowski,23819,false) + im: scala.reflect.runtime.universe.InstanceMirror = instance mirror for Purchase(Jeff Lebowski,23819,false) scala> val shippingFieldMirror = im.reflectField(shippingTermSymb) - shippingFieldMirror: reflect.runtime.universe.FieldMirror = field mirror for Purchase.shipped (bound to Purchase(Jeff Lebowski,23819,false)) + shippingFieldMirror: scala.reflect.runtime.universe.FieldMirror = field mirror for Purchase.shipped (bound to Purchase(Jeff Lebowski,23819,false)) ある特定のインスタンスの `shipped` メンバにアクセスするためには、その特定のインスタンス `p` のためのミラー `im` を必要とする。 @@ -241,7 +241,7 @@ Scala のランタイム型は全てのコンパイル時の型情報を保持 | val rightTag = ru.typeTag[S] | leftTag.tpe <:< rightTag.tpe | } - m: [T, S](x: T, y: S)(implicit evidence$1: reflect.runtime.universe.TypeTag[T], implicit evidence$2: reflect.runtime.universe.TypeTag[S])Boolean + m: [T, S](x: T, y: S)(implicit evidence$1: scala.reflect.runtime.universe.TypeTag[T], implicit evidence$2: scala.reflect.runtime.universe.TypeTag[S])Boolean scala> m(d, c) res9: Boolean = true diff --git a/ja/overviews/reflection/symbols-trees-types.md b/ja/overviews/reflection/symbols-trees-types.md index aa10eb4db3..b3995121a0 100644 --- a/ja/overviews/reflection/symbols-trees-types.md +++ b/ja/overviews/reflection/symbols-trees-types.md @@ -64,20 +64,20 @@ title: シンボル、構文木、型 具体例を用いて説明しよう。 - scala> import reflect.runtime.universe._ - import reflect.runtime.universe._ + scala> import scala.reflect.runtime.universe._ + import scala.reflect.runtime.universe._ scala> class C[T] { def test[U](x: T)(y: U): Int = ??? } defined class C scala> val testMember = typeOf[C[Int]].member(newTermName("test")) - testMember: reflect.runtime.universe.Symbol = method test + testMember: scala.reflect.runtime.universe.Symbol = method test この場合、`member` は期待される `MethodSymbol` ではなく `Symbol` のインスタンスを返す。 このため、`asMethod` を使って `MethodSymbol` が返されたことを保証する必要がある。 scala> testMember.asMethod - res0: reflect.runtime.universe.MethodSymbol = method test + res0: scala.reflect.runtime.universe.MethodSymbol = method test ### 自由シンボル @@ -118,7 +118,7 @@ title: シンボル、構文木、型 import scala.reflect.runtime.universe._ scala> typeOf[List[Int]] - res0: reflect.runtime.universe.Type = scala.List[Int] + res0: scala.reflect.runtime.universe.Type = scala.List[Int] この例では、型コンストラクタ `List` に型引数 `Int` が適用された [`scala.reflect.api.Types$TypeRef`](http://www.scala-lang.org/api/{{ site.scala-version }}/scala/reflect/api/Types$TypeRef.html) @@ -130,10 +130,10 @@ title: シンボル、構文木、型 を生成し、そこから任意のインスタンスに対する型を取得することができる: scala> def getType[T: TypeTag](obj: T) = typeOf[T] - getType: [T](obj: T)(implicit evidence$1: reflect.runtime.universe.TypeTag[T])reflect.runtime.universe.Type + getType: [T](obj: T)(implicit evidence$1: scala.reflect.runtime.universe.TypeTag[T])scala.reflect.runtime.universe.Type scala> getType(List(1,2,3)) - res1: reflect.runtime.universe.Type = List[Int] + res1: scala.reflect.runtime.universe.Type = List[Int] scala> class Animal; class Cat extends Animal defined class Animal @@ -143,13 +143,13 @@ title: シンボル、構文木、型 a: Animal = Animal@21c17f5a scala> getType(a) - res2: reflect.runtime.universe.Type = Animal + res2: scala.reflect.runtime.universe.Type = Animal scala> val c = new Cat c: Cat = Cat@2302d72d scala> getType(c) - res3: reflect.runtime.universe.Type = Cat + res3: scala.reflect.runtime.universe.Type = Cat **注意**: `typeOf` メソッドは、型パラメータを受け取る型 (例えば、`A` が型パラメータであるとき `typeOf[List[A]]`) では動作しない。 @@ -166,7 +166,7 @@ title: シンボル、構文木、型 import scala.reflect.runtime.universe scala> val intTpe = universe.definitions.IntTpe - intTpe: reflect.runtime.universe.Type = Int + intTpe: scala.reflect.runtime.universe.Type = Int 標準型のリストは [`scala.reflect.api.StandardDefinitions`](http://www.scala-lang.org/api/current/index.html#scala.reflect.api.StandardDefinitions$StandardTypes) 内の `StandardTypes` トレイトにて定義されている。 @@ -255,7 +255,7 @@ Scala の数値型は以下の順序付けに従っている (Scala 言語仕様 import scala.reflect.runtime.universe._ scala> def getType[T: TypeTag](obj: T) = typeOf[T] - getType: [T](obj: T)(implicit evidence$1: reflect.runtime.universe.TypeTag[T])reflect.runtime.universe.Type + getType: [T](obj: T)(implicit evidence$1: scala.reflect.runtime.universe.TypeTag[T])scala.reflect.runtime.universe.Type scala> class A defined class A @@ -327,13 +327,13 @@ Scala の数値型は以下の順序付けに従っている (Scala 言語仕様 import scala.reflect.runtime.universe._ scala> typeOf[List[_]].member("map": TermName) - res0: reflect.runtime.universe.Symbol = method map + res0: scala.reflect.runtime.universe.Symbol = method map メソッドを照会するために `member` メソッドに `TermName` を渡していることに注意してほしい。 ここで、`List` の自分型である `Self` のような型メンバを照会する場合は `TypeName` を渡す: scala> typeOf[List[_]].member("Self": TypeName) - res1: reflect.runtime.universe.Symbol = type Self + res1: scala.reflect.runtime.universe.Symbol = type Self 型の全てのメンバや宣言を面白い方法で照会することもできる。 `members` メソッドを使って、渡された型の全ての継承もしくは宣言されたメンバを表す @@ -387,7 +387,7 @@ Scala リフレクションは、ユニバース経由で構文木を視覚化 import scala.reflect.runtime.universe._ scala> val tree = Apply(Select(Ident(newTermName("x")), newTermName("$plus")), List(Literal(Constant(2)))) - tree: reflect.runtime.universe.Apply = x.$plus(2) + tree: scala.reflect.runtime.universe.Apply = x.$plus(2) `show` メソッド (もしくは同等の `toString`) を使ってこの構文木が何を表しているかを見てみよう。 @@ -404,14 +404,14 @@ Scala リフレクションは、ユニバース経由で構文木を視覚化 import scala.reflect.runtime.universe._ scala> val expr = reify { class Flower { def name = "Rose" } } - expr: reflect.runtime.universe.Expr[Unit] = ... + expr: scala.reflect.runtime.universe.Expr[Unit] = ... ここで、`reify` は Scala 式を受け取り `Tree` と `TypeTag` をラッピングする `Expr` を返す。 (`Expr` の詳細に関してはこのガイドの[式]({{ site.baseurl }}/ja/overviews/reflection/names-exprs-scopes-more.html)の節を参照) `expr` が保持する構文木は以下のように取得できる: scala> val tree = expr.tree - tree: reflect.runtime.universe.Tree = + tree: scala.reflect.runtime.universe.Tree = { class Flower extends AnyRef { def () = { @@ -448,35 +448,35 @@ Scala リフレクションは、ユニバース経由で構文木を視覚化 import scala.reflect.runtime.universe._ scala> val tree = Apply(Select(Ident(newTermName("x")), newTermName("$plus")), List(Literal(Constant(2)))) - tree: reflect.runtime.universe.Apply = x.$plus(2) + tree: scala.reflect.runtime.universe.Apply = x.$plus(2) `tree` に対してマッチをかけてやるだけでよく、`Apply` ケースの場合には `Apply` の関数と引数を返す: scala> val (fun, arg) = tree match { | case Apply(fn, a :: Nil) => (fn, a) | } - fun: reflect.runtime.universe.Tree = x.$plus - arg: reflect.runtime.universe.Tree = 2 + fun: scala.reflect.runtime.universe.Tree = x.$plus + arg: scala.reflect.runtime.universe.Tree = 2 パターンマッチを左辺項に移すことで上記と同じことをより簡潔に実現できる: scala> val Apply(fun, arg :: Nil) = tree - fun: reflect.runtime.universe.Tree = x.$plus - arg: reflect.runtime.universe.Tree = 2 + fun: scala.reflect.runtime.universe.Tree = x.$plus + arg: scala.reflect.runtime.universe.Tree = 2 ノードは他のノード内に任意の深さで入れ子になることができるため、`Tree` は普通かなり複雑となることに注意してほしい。これを示す簡単な例として、上記の構文木に 2つ目の `Apply` を加えて既にある和に `3` を加算する: scala> val tree = Apply(Select(Apply(Select(Ident(newTermName("x")), newTermName("$plus")), List(Literal(Constant(2)))), newTermName("$plus")), List(Literal(Constant(3)))) - tree: reflect.runtime.universe.Apply = x.$plus(2).$plus(3) + tree: scala.reflect.runtime.universe.Apply = x.$plus(2).$plus(3) これに上記と同じパターンマッチを適用すると外側の `Apply` ノードが得られ、それは上で見た `x.$plus(2)` を表す構文木を関数部分として格納する: scala> val Apply(fun, arg :: Nil) = tree - fun: reflect.runtime.universe.Tree = x.$plus(2).$plus - arg: reflect.runtime.universe.Tree = 3 + fun: scala.reflect.runtime.universe.Tree = x.$plus(2).$plus + arg: scala.reflect.runtime.universe.Tree = 3 scala> showRaw(fun) res3: String = Select(Apply(Select(Ident(newTermName("x")), newTermName("$plus")), List(Literal(Constant(2)))), newTermName("$plus")) @@ -499,7 +499,7 @@ Scala リフレクションは、ユニバース経由で構文木を視覚化 import scala.reflect.runtime.universe._ scala> val tree = Apply(Select(Apply(Select(Ident(newTermName("x")), newTermName("$plus")), List(Literal(Constant(2)))), newTermName("$plus")), List(Literal(Constant(3)))) - tree: reflect.runtime.universe.Apply = x.$plus(2).$plus(3) + tree: scala.reflect.runtime.universe.Apply = x.$plus(2).$plus(3) scala> object traverser extends Traverser { | var applies = List[Apply]() @@ -538,7 +538,7 @@ Scala リフレクションは、ユニバース経由で構文木を視覚化 scala> traverser.traverse(tree) scala> traverser.applies - res0: List[reflect.runtime.universe.Apply] = List(x.$plus(2), x.$plus(2).$plus(3)) + res0: List[scala.reflect.runtime.universe.Apply] = List(x.$plus(2), x.$plus(2).$plus(3)) ### 構文木の構築 @@ -582,10 +582,10 @@ Scala リフレクションでは、`reify` メソッドを用いた構文木の 例えば、`splice` を用いて `println(2)` を表す構文木を構築してみよう: scala> val x = reify(2) - x: reflect.runtime.universe.Expr[Int(2)] = Expr[Int(2)](2) + x: scala.reflect.runtime.universe.Expr[Int(2)] = Expr[Int(2)](2) scala> reify(println(x.splice)) - res1: reflect.runtime.universe.Expr[Unit] = Expr[Unit](scala.this.Predef.println(2)) + res1: scala.reflect.runtime.universe.Expr[Unit] = Expr[Unit](scala.this.Predef.println(2)) ここで `2` と `println` をそれぞれ別に `reify` して、一方を他方の中に `splice` している。 @@ -593,7 +593,7 @@ Scala リフレクションでは、`reify` メソッドを用いた構文木の `println` の引数の代わりに、`println` そのものを抽象化しようとした場合は失敗することを以下に示す: scala> val fn = reify(println) - fn: reflect.runtime.universe.Expr[Unit] = Expr[Unit](scala.this.Predef.println()) + fn: scala.reflect.runtime.universe.Expr[Unit] = Expr[Unit](scala.this.Predef.println()) scala> reify(fn.splice(2)) :12: error: Unit does not take parameters @@ -620,7 +620,7 @@ Scala リフレクションでは、`reify` メソッドを用いた構文木の import scala.tools.reflect.ToolBox scala> val tb = runtimeMirror(getClass.getClassLoader).mkToolBox() - tb: scala.tools.reflect.ToolBox[reflect.runtime.universe.type] = scala.tools.reflect.ToolBoxFactory$ToolBoxImpl@7bc979dd + tb: scala.tools.reflect.ToolBox[scala.reflect.runtime.universe.type] = scala.tools.reflect.ToolBoxFactory$ToolBoxImpl@7bc979dd scala> showRaw(tb.parse("println(2)")) res2: String = Apply(Ident(newTermName("println")), List(Literal(Constant(2)))) @@ -632,8 +632,8 @@ Scala リフレクションでは、`reify` メソッドを用いた構文木の **注意**: マクロを使っている場合は、`ToolBox.parse` を使うべきではない。マクロコンテキストに既に `parse` メソッドが組み込まれているからだ。具体例を使って説明しよう: - scala> import language.experimental.macros - import language.experimental.macros + scala> import scala.language.experimental.macros + import scala.language.experimental.macros scala> def impl(c: scala.reflect.macros.Context) = c.Expr[Unit](c.parse("println(2)")) impl: (c: scala.reflect.macros.Context)c.Expr[Unit] @@ -659,13 +659,13 @@ Scala リフレクションでは、`reify` メソッドを用いた構文木の import scala.reflect.runtime.universe._ scala> val tree = reify { "test".length }.tree - tree: reflect.runtime.universe.Tree = "test".length() + tree: scala.reflect.runtime.universe.Tree = "test".length() scala> import scala.tools.reflect.ToolBox import scala.tools.reflect.ToolBox scala> val tb = runtimeMirror(getClass.getClassLoader).mkToolBox() - tb: scala.tools.reflect.ToolBox[reflect.runtime.universe.type] = ... + tb: scala.tools.reflect.ToolBox[scala.reflect.runtime.universe.type] = ... scala> val ttree = tb.typeCheck(tree) ttree: tb.u.Tree = "test".length() @@ -688,7 +688,7 @@ Scala リフレクションでは、`reify` メソッドを用いた構文木の `println(2)` を使った例題を手動で構築すると、こうなる: scala> Apply(Ident(newTermName("println")), List(Literal(Constant(2)))) - res0: reflect.runtime.universe.Apply = println(2) + res0: scala.reflect.runtime.universe.Apply = println(2) このテクニックの典型的なユースケースは単独では意味を成さない動的に構築された部分木を組み合わせて構文木を作る必要がある場合だ。 そのような場合、引数が型付けられていることを必要とする `reify` はおそらく不適切だろう。 diff --git a/overviews/quasiquotes/hygiene.md b/overviews/quasiquotes/hygiene.md index 6ffa1932a5..6545141026 100644 --- a/overviews/quasiquotes/hygiene.md +++ b/overviews/quasiquotes/hygiene.md @@ -36,8 +36,8 @@ Here we can see that plain reference to `Map` doesn\'t respect our custom import // ---- MyMacro.scala ---- package example - import reflect.macros.blackbox.Context - import language.experimental.macros + import scala.reflect.macros.blackbox.Context + import scala.language.experimental.macros object MyMacro { def wrapper(x: Int) = { println(s"wrapped x = $x"); x } diff --git a/overviews/quasiquotes/lifting.md b/overviews/quasiquotes/lifting.md index 8105e15ce3..269f7a41da 100644 --- a/overviews/quasiquotes/lifting.md +++ b/overviews/quasiquotes/lifting.md @@ -114,8 +114,8 @@ into a case class constructor call. In this example there two important points t Due to path dependent nature of current reflection API it isn't trivial to share the same Liftable definition between both macro and runtime universes. A possible way to do this is to define Liftable implementations in a trait and instantiate it for each universe separately: - import reflect.api.Universe - import reflect.macros.blackbox.Context + import scala.reflect.api.Universe + import scala.reflect.macros.blackbox.Context trait LiftableImpls { val universe: Universe @@ -142,7 +142,7 @@ Due to path dependent nature of current reflection API it isn't trivial to share So in practice it's much easier to just define a liftable for given universe at hand: - import reflect.macros.blackbox.Context + import scala.reflect.macros.blackbox.Context // macro impls defined as a macro bundle class MyMacros(c: Context) { diff --git a/overviews/quasiquotes/setup.md b/overviews/quasiquotes/setup.md index a10e9c47e0..873ab086a9 100644 --- a/overviews/quasiquotes/setup.md +++ b/overviews/quasiquotes/setup.md @@ -15,15 +15,15 @@ In Scala 2.11, quasiquotes are shipped in the official Scala distribution as par All examples and code snippets in this guide are run under in 2.11 REPL with one extra line: - scala> val universe: reflect.runtime.universe.type = reflect.runtime.universe + scala> val universe: scala.reflect.runtime.universe.type = scala.reflect.runtime.universe scala> import universe._ A wildcard import from a universe (be it a runtime reflection universe like here or a compile-time universe provided in macros) is all that's needed to use quasiquotes. All of the examples will assume that import. Additionally some examples that use `ToolBox` API will need a few more lines to get things rolling: - scala> import reflect.runtime.currentMirror - scala> import tools.reflect.ToolBox + scala> import scala.reflect.runtime.currentMirror + scala> import scala.tools.reflect.ToolBox scala> val toolbox = currentMirror.mkToolBox() Another tool you might want to be aware of is new and shiny `showCode` pretty printer (contributed by [@VladimirNik](https://github.com/VladimirNik)): diff --git a/overviews/reflection/annotations-names-scopes.md b/overviews/reflection/annotations-names-scopes.md index b56b9fd895..4fdffe525e 100644 --- a/overviews/reflection/annotations-names-scopes.md +++ b/overviews/reflection/annotations-names-scopes.md @@ -70,7 +70,7 @@ Names are associated with a universe. Example: import scala.reflect.runtime.universe._ scala> val mapName = newTermName("map") - mapName: reflect.runtime.universe.TermName = map + mapName: scala.reflect.runtime.universe.TermName = map Above, we're creating a `Name` associated with the runtime reflection universe (this is also visible in its path-dependent type @@ -80,17 +80,17 @@ Names are often used to look up members of types. For example, to search for the `map` method (which is a term) declared in the `List` class, one can do: scala> val listTpe = typeOf[List[Int]] - listTpe: reflect.runtime.universe.Type = scala.List[Int] + listTpe: scala.reflect.runtime.universe.Type = scala.List[Int] scala> listTpe.member(mapName) - res1: reflect.runtime.universe.Symbol = method map + res1: scala.reflect.runtime.universe.Symbol = method map To search for a type member, one can follow the same procedure, using `newTypeName` instead. It is also possible to rely on implicit conversions to convert between strings and term or type names: scala> listTpe.member("map": TermName) - res2: reflect.runtime.universe.Symbol = method map + res2: scala.reflect.runtime.universe.Symbol = method map ### Standard Names @@ -127,7 +127,7 @@ The following example returns a list of the symbols of all overridden members of the `List` class, in declaration order: scala> val overridden = listTpe.declarations.sorted.filter(_.isOverride) - overridden: List[reflect.runtime.universe.Symbol] = List(method companion, method ++, method +:, method toList, method take, method drop, method slice, method takeRight, method splitAt, method takeWhile, method dropWhile, method span, method reverse, method stringPrefix, method toStream, method foreach) + overridden: List[scala.reflect.runtime.universe.Symbol] = List(method companion, method ++, method +:, method toList, method take, method drop, method slice, method takeRight, method splitAt, method takeWhile, method dropWhile, method span, method reverse, method stringPrefix, method toStream, method foreach) ## Exprs @@ -303,7 +303,7 @@ representation of Scala code. For example: import scala.reflect.runtime.universe._ scala> def tree = reify { final class C { def x = 2 } }.tree - tree: reflect.runtime.universe.Tree + tree: scala.reflect.runtime.universe.Tree scala> show(tree) res0: String = @@ -380,7 +380,7 @@ The method `show` can be used to produce a *readable* string representation of a import scala.reflect.runtime.universe._ scala> def tpe = typeOf[{ def x: Int; val y: List[Int] }] - tpe: reflect.runtime.universe.Type + tpe: scala.reflect.runtime.universe.Type scala> show(tpe) res0: String = scala.AnyRef{def x: Int; val y: scala.List[Int]} diff --git a/overviews/reflection/environment-universes-mirrors.md b/overviews/reflection/environment-universes-mirrors.md index 13691b5847..083bd92951 100644 --- a/overviews/reflection/environment-universes-mirrors.md +++ b/overviews/reflection/environment-universes-mirrors.md @@ -79,7 +79,7 @@ A `ReflectiveMirror` is used for loading symbols by name, and as an entry point ru: scala.reflect.api.JavaUniverse = ... scala> val m = ru.runtimeMirror(getClass.getClassLoader) - m: reflect.runtime.universe.Mirror = JavaMirror ... + m: scala.reflect.runtime.universe.Mirror = JavaMirror ... An `InstanceMirror` is used for creating invoker mirrors for methods and fields and for inner classes and inner objects (modules). Entry point: `val im = m.reflect()`. Example: @@ -87,15 +87,15 @@ An `InstanceMirror` is used for creating invoker mirrors for methods and fields defined class C scala> val im = m.reflect(new C) - im: reflect.runtime.universe.InstanceMirror = instance mirror for C@3442299e + im: scala.reflect.runtime.universe.InstanceMirror = instance mirror for C@3442299e A `MethodMirror` is used for invoking instance methods (Scala only has instance methods-- methods of objects are instance methods of object instances, obtainable via `ModuleMirror.instance`). Entry point: `val mm = im.reflectMethod()`. Example: scala> val methodX = ru.typeOf[C].declaration(ru.newTermName("x")).asMethod - methodX: reflect.runtime.universe.MethodSymbol = method x + methodX: scala.reflect.runtime.universe.MethodSymbol = method x scala> val mm = im.reflectMethod(methodX) - mm: reflect.runtime.universe.MethodMirror = method mirror for C.x: scala.Int (bound to C@3442299e) + mm: scala.reflect.runtime.universe.MethodMirror = method mirror for C.x: scala.Int (bound to C@3442299e) scala> mm() res0: Any = 2 @@ -106,16 +106,16 @@ A `FieldMirror` is used for getting/setting instance fields (like methods, Scala defined class C scala> val m = ru.runtimeMirror(getClass.getClassLoader) - m: reflect.runtime.universe.Mirror = JavaMirror ... + m: scala.reflect.runtime.universe.Mirror = JavaMirror ... scala> val im = m.reflect(new C) - im: reflect.runtime.universe.InstanceMirror = instance mirror for C@5f0c8ac1 + im: scala.reflect.runtime.universe.InstanceMirror = instance mirror for C@5f0c8ac1 scala> val fieldX = ru.typeOf[C].declaration(ru.newTermName("x")).asTerm.accessed.asTerm - fieldX: reflect.runtime.universe.TermSymbol = value x + fieldX: scala.reflect.runtime.universe.TermSymbol = value x scala> val fmX = im.reflectField(fieldX) - fmX: reflect.runtime.universe.FieldMirror = field mirror for C.x (bound to C@5f0c8ac1) + fmX: scala.reflect.runtime.universe.FieldMirror = field mirror for C.x (bound to C@5f0c8ac1) scala> fmX.get res0: Any = 2 @@ -123,10 +123,10 @@ A `FieldMirror` is used for getting/setting instance fields (like methods, Scala scala> fmX.set(3) scala> val fieldY = ru.typeOf[C].declaration(ru.newTermName("y")).asTerm.accessed.asTerm - fieldY: reflect.runtime.universe.TermSymbol = variable y + fieldY: scala.reflect.runtime.universe.TermSymbol = variable y scala> val fmY = im.reflectField(fieldY) - fmY: reflect.runtime.universe.FieldMirror = field mirror for C.y (bound to C@5f0c8ac1) + fmY: scala.reflect.runtime.universe.FieldMirror = field mirror for C.y (bound to C@5f0c8ac1) scala> fmY.get res1: Any = 3 @@ -142,19 +142,19 @@ A `ClassMirror` is used for creating invoker mirrors for constructors. Entry poi defined class C scala> val m = ru.runtimeMirror(getClass.getClassLoader) - m: reflect.runtime.universe.Mirror = JavaMirror ... + m: scala.reflect.runtime.universe.Mirror = JavaMirror ... scala> val classC = ru.typeOf[C].typeSymbol.asClass - classC: reflect.runtime.universe.Symbol = class C + classC: scala.reflect.runtime.universe.Symbol = class C scala> val cm = m.reflectClass(classC) - cm: reflect.runtime.universe.ClassMirror = class mirror for C (bound to null) + cm: scala.reflect.runtime.universe.ClassMirror = class mirror for C (bound to null) scala> val ctorC = ru.typeOf[C].declaration(ru.nme.CONSTRUCTOR).asMethod - ctorC: reflect.runtime.universe.MethodSymbol = constructor C + ctorC: scala.reflect.runtime.universe.MethodSymbol = constructor C scala> val ctorm = cm.reflectConstructor(ctorC) - ctorm: reflect.runtime.universe.MethodMirror = constructor mirror for C.(x: scala.Int): C (bound to null) + ctorm: scala.reflect.runtime.universe.MethodMirror = constructor mirror for C.(x: scala.Int): C (bound to null) scala> ctorm(2) res0: Any = C(2) @@ -165,13 +165,13 @@ A `ModuleMirror` is used for accessing instances of singleton objects. Entry poi defined module C scala> val m = ru.runtimeMirror(getClass.getClassLoader) - m: reflect.runtime.universe.Mirror = JavaMirror ... + m: scala.reflect.runtime.universe.Mirror = JavaMirror ... scala> val objectC = ru.typeOf[C.type].termSymbol.asModule - objectC: reflect.runtime.universe.ModuleSymbol = object C + objectC: scala.reflect.runtime.universe.ModuleSymbol = object C scala> val mm = m.reflectModule(objectC) - mm: reflect.runtime.universe.ModuleMirror = module mirror for C (bound to null) + mm: scala.reflect.runtime.universe.ModuleMirror = module mirror for C (bound to null) scala> val obj = mm.instance obj: Any = C$@1005ec04 diff --git a/overviews/reflection/overview.md b/overviews/reflection/overview.md index f60d277282..8f068d43dd 100644 --- a/overviews/reflection/overview.md +++ b/overviews/reflection/overview.md @@ -113,30 +113,30 @@ walk through an example using the REPL: defined class Person scala> val m = ru.runtimeMirror(getClass.getClassLoader) - m: reflect.runtime.universe.Mirror = JavaMirror with ... + m: scala.reflect.runtime.universe.Mirror = JavaMirror with ... In the first step we obtain a mirror `m` which makes all classes and types available that are loaded by the current classloader, including class `Person`. scala> val classPerson = ru.typeOf[Person].typeSymbol.asClass - classPerson: reflect.runtime.universe.ClassSymbol = class Person + classPerson: scala.reflect.runtime.universe.ClassSymbol = class Person scala> val cm = m.reflectClass(classPerson) - cm: reflect.runtime.universe.ClassMirror = class mirror for Person (bound to null) + cm: scala.reflect.runtime.universe.ClassMirror = class mirror for Person (bound to null) The second step involves obtaining a `ClassMirror` for class `Person` using the `reflectClass` method. The `ClassMirror` provides access to the constructor of class `Person`. scala> val ctor = ru.typeOf[Person].declaration(ru.nme.CONSTRUCTOR).asMethod - ctor: reflect.runtime.universe.MethodSymbol = constructor Person + ctor: scala.reflect.runtime.universe.MethodSymbol = constructor Person The symbol for `Person`s constructor can be obtained using only the runtime universe `ru` by looking it up in the declarations of type `Person`. scala> val ctorm = cm.reflectConstructor(ctor) - ctorm: reflect.runtime.universe.MethodMirror = constructor mirror for Person.(name: String): Person (bound to null) + ctorm: scala.reflect.runtime.universe.MethodMirror = constructor mirror for Person.(name: String): Person (bound to null) scala> val p = ctorm("Mike") p: Any = Person(Mike) @@ -161,7 +161,7 @@ In this example, we will attempt to get and set the `shipped` field of import scala.reflect.runtime.{universe=>ru} scala> val m = ru.runtimeMirror(p.getClass.getClassLoader) - m: reflect.runtime.universe.Mirror = JavaMirror with ... + m: scala.reflect.runtime.universe.Mirror = JavaMirror with ... As we did in the previous example, we'll begin by obtaining a mirror `m`, which makes all classes and types available that are loaded by the classloader @@ -169,7 +169,7 @@ that also loaded the class of `p` (`Purchase`), which we need in order to access member `shipped`. scala> val shippingTermSymb = ru.typeOf[Purchase].declaration(ru.newTermName("shipped")).asTerm - shippingTermSymb: reflect.runtime.universe.TermSymbol = method shipped + shippingTermSymb: scala.reflect.runtime.universe.TermSymbol = method shipped We now look up the declaration of the `shipped` field, which gives us a `TermSymbol` (a type of `Symbol`). We'll need to use this `Symbol` later to @@ -177,10 +177,10 @@ obtain a mirror that gives us access to the value of this field (for some instance). scala> val im = m.reflect(p) - im: reflect.runtime.universe.InstanceMirror = instance mirror for Purchase(Jeff Lebowski,23819,false) + im: scala.reflect.runtime.universe.InstanceMirror = instance mirror for Purchase(Jeff Lebowski,23819,false) scala> val shippingFieldMirror = im.reflectField(shippingTermSymb) - shippingFieldMirror: reflect.runtime.universe.FieldMirror = field mirror for Purchase.shipped (bound to Purchase(Jeff Lebowski,23819,false)) + shippingFieldMirror: scala.reflect.runtime.universe.FieldMirror = field mirror for Purchase.shipped (bound to Purchase(Jeff Lebowski,23819,false)) In order to access a specific instance's `shipped` member, we need a mirror for our specific instance, `p`'s instance mirror, `im`. Given our instance @@ -272,7 +272,7 @@ it returns `true`. | val rightTag = ru.typeTag[S] | leftTag.tpe <:< rightTag.tpe | } - m: [T, S](x: T, y: S)(implicit evidence$1: reflect.runtime.universe.TypeTag[T], implicit evidence$2: reflect.runtime.universe.TypeTag[S])Boolean + m: [T, S](x: T, y: S)(implicit evidence$1: scala.reflect.runtime.universe.TypeTag[T], implicit evidence$2: scala.reflect.runtime.universe.TypeTag[S])Boolean scala> m(d, c) res9: Boolean = true diff --git a/overviews/reflection/symbols-trees-types.md b/overviews/reflection/symbols-trees-types.md index 1cc46ee448..e0cd5054ac 100644 --- a/overviews/reflection/symbols-trees-types.md +++ b/overviews/reflection/symbols-trees-types.md @@ -75,21 +75,21 @@ more specific subtype of `Symbol` as appropriate (if you want to use the For example, - scala> import reflect.runtime.universe._ - import reflect.runtime.universe._ + scala> import scala.reflect.runtime.universe._ + import scala.reflect.runtime.universe._ scala> class C[T] { def test[U](x: T)(y: U): Int = ??? } defined class C scala> val testMember = typeOf[C[Int]].member(newTermName("test")) - testMember: reflect.runtime.universe.Symbol = method test + testMember: scala.reflect.runtime.universe.Symbol = method test In this case, `member` returns an instance of `Symbol`, not `MethodSymbol` as one might expect. Thus, we must use `asMethod` to ensure that we obtain a `MethodSymbol` scala> testMember.asMethod - res0: reflect.runtime.universe.MethodSymbol = method test + res0: scala.reflect.runtime.universe.MethodSymbol = method test ### Free symbols @@ -134,7 +134,7 @@ example: import scala.reflect.runtime.universe._ scala> typeOf[List[Int]] - res0: reflect.runtime.universe.Type = scala.List[Int] + res0: scala.reflect.runtime.universe.Type = scala.List[Int] In this example, a [`scala.reflect.api.Types$TypeRef`](http://www.scala-lang.org/api/{{ site.scala-version }}/scala/reflect/api/Types$TypeRef.html) @@ -149,10 +149,10 @@ specialized `TypeTag` for us, which we can use to obtain the type of our arbitrary instance: scala> def getType[T: TypeTag](obj: T) = typeOf[T] - getType: [T](obj: T)(implicit evidence$1: reflect.runtime.universe.TypeTag[T])reflect.runtime.universe.Type + getType: [T](obj: T)(implicit evidence$1: scala.reflect.runtime.universe.TypeTag[T])scala.reflect.runtime.universe.Type scala> getType(List(1,2,3)) - res1: reflect.runtime.universe.Type = List[Int] + res1: scala.reflect.runtime.universe.Type = List[Int] scala> class Animal; class Cat extends Animal defined class Animal @@ -162,13 +162,13 @@ arbitrary instance: a: Animal = Animal@21c17f5a scala> getType(a) - res2: reflect.runtime.universe.Type = Animal + res2: scala.reflect.runtime.universe.Type = Animal scala> val c = new Cat c: Cat = Cat@2302d72d scala> getType(c) - res3: reflect.runtime.universe.Type = Cat + res3: scala.reflect.runtime.universe.Type = Cat _Note:_ Method `typeOf` does not work for types with type parameters, such as `typeOf[List[A]]` where `A` is a type parameter. In this case, one can use @@ -184,7 +184,7 @@ Standard types, such as `Int`, `Boolean`, `Any`, or `Unit`, are accessible throu import scala.reflect.runtime.universe scala> val intTpe = universe.definitions.IntTpe - intTpe: reflect.runtime.universe.Type = Int + intTpe: scala.reflect.runtime.universe.Type = Int The list of standard types is specified in trait `StandardTypes` in [`scala.reflect.api.StandardDefinitions`](http://www.scala-lang.org/api/current/index.html#scala.reflect.api.StandardDefinitions$StandardTypes). @@ -204,7 +204,7 @@ Given two `Type` instances, one can easily test whether one is a subtype of the other using `<:<` (and in exceptional cases, `weak_<:<`, explained below) scala> import scala.reflect.runtime.universe._ - import scala-lang.reflect.runtime.universe._ + import scala.reflect.runtime.universe._ scala> class A; class B extends A defined class A @@ -276,7 +276,7 @@ denote the exact same compile-time type. import scala.reflect.runtime.universe._ scala> def getType[T: TypeTag](obj: T) = typeOf[T] - getType: [T](obj: T)(implicit evidence$1: reflect.runtime.universe.TypeTag[T])reflect.runtime.universe.Type + getType: [T](obj: T)(implicit evidence$1: scala.reflect.runtime.universe.TypeTag[T])scala.reflect.runtime.universe.Type scala> class A defined class A @@ -351,14 +351,14 @@ For example, to look up the `map` method of `List`, one can do: import scala.reflect.runtime.universe._ scala> typeOf[List[_]].member("map": TermName) - res0: reflect.runtime.universe.Symbol = method map + res0: scala.reflect.runtime.universe.Symbol = method map Note that we pass method `member` a `TermName`, since we're looking up a method. If we were to look up a type member, such as `List`'s self type, `Self`, we would pass a `TypeName`: scala> typeOf[List[_]].member("Self": TypeName) - res1: reflect.runtime.universe.Symbol = type Self + res1: scala.reflect.runtime.universe.Symbol = type Self We can also query all members or declarations on a type in interesting ways. We can use method `members` to obtain a `Traversable` (`MemberScopeApi` @@ -418,7 +418,7 @@ For example, given the following tree: import scala.reflect.runtime.universe._ scala> val tree = Apply(Select(Ident(newTermName("x")), newTermName("$plus")), List(Literal(Constant(2)))) - tree: reflect.runtime.universe.Apply = x.$plus(2) + tree: scala.reflect.runtime.universe.Apply = x.$plus(2) We can use method `show` (or `toString`, which is equivalent) to see what that tree represents. @@ -437,7 +437,7 @@ expression: import scala.reflect.runtime.universe._ scala> val expr = reify { class Flower { def name = "Rose" } } - expr: reflect.runtime.universe.Expr[Unit] = ... + expr: scala.reflect.runtime.universe.Expr[Unit] = ... Here, `reify` simply takes the Scala expression it was passed, and returns a Scala `Expr`, which is simply wraps a `Tree` and a `TypeTag` (see the @@ -446,7 +446,7 @@ section of this guide for more information about `Expr`s). We can obtain the tree that `expr` contains by: scala> val tree = expr.tree - tree: reflect.runtime.universe.Tree = + tree: scala.reflect.runtime.universe.Tree = { class Flower extends AnyRef { def () = { @@ -484,7 +484,7 @@ in the following tree: import scala.reflect.runtime.universe._ scala> val tree = Apply(Select(Ident(newTermName("x")), newTermName("$plus")), List(Literal(Constant(2)))) - tree: reflect.runtime.universe.Apply = x.$plus(2) + tree: scala.reflect.runtime.universe.Apply = x.$plus(2) We can simply match on our `tree`, and in the case that we have an `Apply` node, just return `Apply`'s function and argument: @@ -492,15 +492,15 @@ node, just return `Apply`'s function and argument: scala> val (fun, arg) = tree match { | case Apply(fn, a :: Nil) => (fn, a) | } - fun: reflect.runtime.universe.Tree = x.$plus - arg: reflect.runtime.universe.Tree = 2 + fun: scala.reflect.runtime.universe.Tree = x.$plus + arg: scala.reflect.runtime.universe.Tree = 2 We can achieve exactly the same thing a bit more concisely, by putting the pattern match on the left-hand side: scala> val Apply(fun, arg :: Nil) = tree - fun: reflect.runtime.universe.Tree = x.$plus - arg: reflect.runtime.universe.Tree = 2 + fun: scala.reflect.runtime.universe.Tree = x.$plus + arg: scala.reflect.runtime.universe.Tree = 2 Note that `Tree`s can typically be quite complex, with nodes nested arbitrarily deep within other nodes. A simple illustration would be if we were @@ -508,15 +508,15 @@ to add a second `Apply` node to the above tree which serves to add `3` to our sum: scala> val tree = Apply(Select(Apply(Select(Ident(newTermName("x")), newTermName("$plus")), List(Literal(Constant(2)))), newTermName("$plus")), List(Literal(Constant(3)))) - tree: reflect.runtime.universe.Apply = x.$plus(2).$plus(3) + tree: scala.reflect.runtime.universe.Apply = x.$plus(2).$plus(3) If we apply the same pattern match as above, we obtain the outer `Apply` node which contains as its function the entire tree representing `x.$plus(2)` that we saw above: scala> val Apply(fun, arg :: Nil) = tree - fun: reflect.runtime.universe.Tree = x.$plus(2).$plus - arg: reflect.runtime.universe.Tree = 3 + fun: scala.reflect.runtime.universe.Tree = x.$plus(2).$plus + arg: scala.reflect.runtime.universe.Tree = 3 scala> showRaw(fun) res3: String = Select(Apply(Select(Ident(newTermName("x")), newTermName("$plus")), List(Literal(Constant(2)))), newTermName("$plus")) @@ -546,7 +546,7 @@ all `Apply` nodes, we could do: import scala.reflect.runtime.universe._ scala> val tree = Apply(Select(Apply(Select(Ident(newTermName("x")), newTermName("$plus")), List(Literal(Constant(2)))), newTermName("$plus")), List(Literal(Constant(3)))) - tree: reflect.runtime.universe.Apply = x.$plus(2).$plus(3) + tree: scala.reflect.runtime.universe.Apply = x.$plus(2).$plus(3) scala> object traverser extends Traverser { | var applies = List[Apply]() @@ -590,7 +590,7 @@ nodes, simply do: scala> traverser.traverse(tree) scala> traverser.applies - res0: List[reflect.runtime.universe.Apply] = List(x.$plus(2), x.$plus(2).$plus(3)) + res0: List[scala.reflect.runtime.universe.Apply] = List(x.$plus(2), x.$plus(2).$plus(3)) ### Creating Trees @@ -644,10 +644,10 @@ For example, let's try to construct a tree representing `println(2)` using `splice`: scala> val x = reify(2) - x: reflect.runtime.universe.Expr[Int(2)] = Expr[Int(2)](2) + x: scala.reflect.runtime.universe.Expr[Int(2)] = Expr[Int(2)](2) scala> reify(println(x.splice)) - res1: reflect.runtime.universe.Expr[Unit] = Expr[Unit](scala.this.Predef.println(2)) + res1: scala.reflect.runtime.universe.Expr[Unit] = Expr[Unit](scala.this.Predef.println(2)) Here, we `reify` `2` and `println` separately, and simply `splice` one into the other. @@ -657,7 +657,7 @@ valid and typeable Scala code. If instead of the argument to `println` we wanted to abstract over the `println` itself, it wouldn't be possible: scala> val fn = reify(println) - fn: reflect.runtime.universe.Expr[Unit] = Expr[Unit](scala.this.Predef.println()) + fn: scala.reflect.runtime.universe.Expr[Unit] = Expr[Unit](scala.this.Predef.println()) scala> reify(fn.splice(2)) :12: error: Unit does not take parameters @@ -687,7 +687,7 @@ section: import scala.tools.reflect.ToolBox scala> val tb = runtimeMirror(getClass.getClassLoader).mkToolBox() - tb: scala.tools.reflect.ToolBox[reflect.runtime.universe.type] = scala.tools.reflect.ToolBoxFactory$ToolBoxImpl@7bc979dd + tb: scala.tools.reflect.ToolBox[scala.reflect.runtime.universe.type] = scala.tools.reflect.ToolBoxFactory$ToolBoxImpl@7bc979dd scala> showRaw(tb.parse("println(2)")) res2: String = Apply(Ident(newTermName("println")), List(Literal(Constant(2)))) @@ -701,8 +701,8 @@ method. _Note:_ when using macros, one shouldn’t use `ToolBox.parse`. This is because there’s already a `parse` method built into the macro context. For example: - scala> import language.experimental.macros - import language.experimental.macros + scala> import scala.language.experimental.macros + import scala.language.experimental.macros scala> def impl(c: scala.reflect.macros.Context) = c.Expr[Unit](c.parse("println(2)")) impl: (c: scala.reflect.macros.Context)c.Expr[Unit] @@ -733,13 +733,13 @@ When using the runtime reflection framework, typechecking is implemented by import scala.reflect.runtime.universe._ scala> val tree = reify { "test".length }.tree - tree: reflect.runtime.universe.Tree = "test".length() + tree: scala.reflect.runtime.universe.Tree = "test".length() scala> import scala.tools.reflect.ToolBox import scala.tools.reflect.ToolBox scala> val tb = runtimeMirror(getClass.getClassLoader).mkToolBox() - tb: scala.tools.reflect.ToolBox[reflect.runtime.universe.type] = ... + tb: scala.tools.reflect.ToolBox[scala.reflect.runtime.universe.type] = ... scala> val ttree = tb.typeCheck(tree) ttree: tb.u.Tree = "test".length() @@ -766,7 +766,7 @@ Our earlier example involving `println(2)` can be manually constructed as follows: scala> Apply(Ident(newTermName("println")), List(Literal(Constant(2)))) - res0: reflect.runtime.universe.Apply = println(2) + res0: scala.reflect.runtime.universe.Apply = println(2) The canonical use case for this technique is when the target tree needs to be assembled from dynamically created parts, which don’t make sense in isolation