diff --git a/ja/overviews/macros/overview.md b/ja/overviews/macros/overview.md index 95783620f5..50eca23dcd 100644 --- a/ja/overviews/macros/overview.md +++ b/ja/overviews/macros/overview.md @@ -51,7 +51,7 @@ def マクロ機能の一部が、徹底した仕様が書かれることを条 Literal(Constant("limit exceeded")) Apply( - Select(Ident(newTermName("x")), newTermName("$less"), + Select(Ident(TermName("x")), TermName("$less"), List(Literal(Constant(10))))) ここに `assert` マクロの実装の一例を載せる: @@ -139,7 +139,7 @@ Scala コードの生成については[リフレクションの概要](http://d val evals = ListBuffer[ValDef]() def precompute(value: Tree, tpe: Type): Ident = { - val freshName = newTermName(c.fresh("eval$")) + val freshName = TermName(c.fresh("eval$")) evals += ValDef(Modifiers(), freshName, TypeTree(tpe), value) Ident(freshName) } @@ -170,7 +170,7 @@ Scala コードの生成については[リフレクションの概要](http://d val evals = ListBuffer[ValDef]() def precompute(value: Tree, tpe: Type): Ident = { - val freshName = newTermName(c.fresh("eval$")) + val freshName = TermName(c.fresh("eval$")) evals += ValDef(Modifiers(), freshName, TypeTree(tpe), value) Ident(freshName) } @@ -260,15 +260,15 @@ Scala コードの生成については[リフレクションの概要](http://d () } Block(List( - ValDef(Modifiers(), newTermName("eval$1"), TypeTree().setType(String), Literal(Constant("world"))), + ValDef(Modifiers(), TermName("eval$1"), TypeTree().setType(String), Literal(Constant("world"))), Apply( - Select(Select(This(newTypeName("scala")), newTermName("Predef")), newTermName("print")), + Select(Select(This(TypeName("scala")), TermName("Predef")), TermName("print")), List(Literal(Constant("hello")))), Apply( - Select(Select(This(newTypeName("scala")), newTermName("Predef")), newTermName("print")), - List(Ident(newTermName("eval$1")))), + Select(Select(This(TypeName("scala")), TermName("Predef")), TermName("print")), + List(Ident(TermName("eval$1")))), Apply( - Select(Select(This(newTypeName("scala")), newTermName("Predef")), newTermName("print")), + Select(Select(This(TypeName("scala")), TermName("Predef")), TermName("print")), List(Literal(Constant("!"))))), Literal(Constant(()))) diff --git a/ja/overviews/macros/typemacros.md b/ja/overviews/macros/typemacros.md index 06db929803..4d897fe440 100644 --- a/ja/overviews/macros/typemacros.md +++ b/ja/overviews/macros/typemacros.md @@ -93,7 +93,7 @@ Scala のプログラムにおいて型マクロは、type、applied type、pare 要点をまとめると、展開された型マクロは型マクロの使用をそれが返す構文木に置き換える。ある展開が理にかなっているかどうかを考えるには、頭の中でマクロの使用例を展開される構文木で置き換えてみて結果のプログラムが正しいか確かめてみればいい。 -例えば、 `class C extends TM(2)(3)` の中で `TM(2)(3)` のように使われている型マクロは `class C extends B(2)` となるように `Apply(Ident(newTypeName("B")), List(Literal(Constant(2))))` と展開することができる。しかし、同じ展開は `TM(2)(3)` が `def x: TM(2)(3) = ???` の中の型として使われた場合は `def x: B(2) = ???` となるため、意味を成さない。(ただし、`B` そのものが型マクロではないとする。その場合は再帰的に展開され、その展開の結果がプログラムの妥当性を決定する。) +例えば、 `class C extends TM(2)(3)` の中で `TM(2)(3)` のように使われている型マクロは `class C extends B(2)` となるように `Apply(Ident(TypeName("B")), List(Literal(Constant(2))))` と展開することができる。しかし、同じ展開は `TM(2)(3)` が `def x: TM(2)(3) = ???` の中の型として使われた場合は `def x: B(2) = ???` となるため、意味を成さない。(ただし、`B` そのものが型マクロではないとする。その場合は再帰的に展開され、その展開の結果がプログラムの妥当性を決定する。) ## コツとトリック diff --git a/ja/overviews/reflection/annotations-names-scopes.md b/ja/overviews/reflection/annotations-names-scopes.md index 3e949bffda..6199b4b8bf 100644 --- a/ja/overviews/reflection/annotations-names-scopes.md +++ b/ja/overviews/reflection/annotations-names-scopes.md @@ -63,7 +63,7 @@ Scala または Java アノテーションに対しては `scalaArgs` は空で scala> import scala.reflect.runtime.universe._ import scala.reflect.runtime.universe._ - scala> val mapName = newTermName("map") + scala> val mapName = TermName("map") mapName: scala.reflect.runtime.universe.TermName = map 上のコードでは、実行時リフレクション・ユニバースに関連付けられた `Name` を作成している。 @@ -77,7 +77,7 @@ Scala または Java アノテーションに対しては `scalaArgs` は空で scala> listTpe.member(mapName) res1: scala.reflect.runtime.universe.Symbol = method map -型メンバを検索するには `newTypeName` を代わりに使って `member` を呼び出す。 +型メンバを検索するには `TypeName` を代わりに使って `member` を呼び出す。 暗黙の変換を使って文字列から項もしくは型の名前に変換することもできる: scala> listTpe.member("map": TermName) @@ -140,16 +140,16 @@ Scala のプログラムにおいて、「`_root_`」のような特定の名前 例えば、`C` という名前のクラスを作るには以下のように書く: - ClassDef(Modifiers(NoFlags), newTypeName("C"), Nil, ...) + ClassDef(Modifiers(NoFlags), TypeName("C"), Nil, ...) ここでフラグ集合は空だ。`C` を private にするには、以下のようにする: - ClassDef(Modifiers(PRIVATE), newTypeName("C"), Nil, ...) + ClassDef(Modifiers(PRIVATE), TypeName("C"), Nil, ...) 垂直バー演算子 (`|`) を使って組み合わせることができる。例えば、private final クラスは以下のように書く: - ClassDef(Modifiers(PRIVATE | FINAL), newTypeName("C"), Nil, ...) + ClassDef(Modifiers(PRIVATE | FINAL), TypeName("C"), Nil, ...) 全てのフラグのリストは `scala.reflect.api.FlagSets#FlagValues` にて定義されており、 @@ -238,7 +238,7 @@ Java の列挙要素への参照はシンボル (`scala.reflect.api.Symbols#Symb object Test extends App { val jann = typeOf[JavaAnnottee].typeSymbol.annotations(0).javaArgs - def jarg(name: String) = jann(newTermName(name)) match { + def jarg(name: String) = jann(TermName(name)) match { // Constant is always wrapped in a Literal or LiteralArgument tree node case LiteralArgument(ct: Constant) => value case _ => sys.error("Not a constant") @@ -301,15 +301,15 @@ Java の列挙要素への参照はシンボル (`scala.reflect.api.Symbols#Symb scala> showRaw(tree) res1: String = Block(List( - ClassDef(Modifiers(FINAL), newTypeName("C"), List(), Template( - List(Ident(newTypeName("AnyRef"))), + ClassDef(Modifiers(FINAL), TypeName("C"), List(), Template( + List(Ident(TypeName("AnyRef"))), emptyValDef, List( DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List( Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(())))), - DefDef(Modifiers(), newTermName("x"), List(), List(), TypeTree(), + DefDef(Modifiers(), TermName("x"), List(), List(), TypeTree(), Literal(Constant(2))))))), Literal(Constant(()))) @@ -323,23 +323,23 @@ Java の列挙要素への参照はシンボル (`scala.reflect.api.Symbols#Symb scala> showRaw(cm.mkToolBox().typeCheck(tree), printTypes = true) res2: String = Block[1](List( - ClassDef[2](Modifiers(FINAL), newTypeName("C"), List(), Template[3]( - List(Ident[4](newTypeName("AnyRef"))), + ClassDef[2](Modifiers(FINAL), TypeName("C"), List(), Template[3]( + List(Ident[4](TypeName("AnyRef"))), emptyValDef, List( DefDef[2](Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree[3](), Block[1](List( - Apply[4](Select[5](Super[6](This[3](newTypeName("C")), tpnme.EMPTY), ...))), + Apply[4](Select[5](Super[6](This[3](TypeName("C")), tpnme.EMPTY), ...))), Literal[1](Constant(())))), - DefDef[2](Modifiers(), newTermName("x"), List(), List(), TypeTree[7](), + DefDef[2](Modifiers(), TermName("x"), List(), List(), TypeTree[7](), Literal[8](Constant(2))))))), Literal[1](Constant(()))) [1] TypeRef(ThisType(scala), scala.Unit, List()) [2] NoType - [3] TypeRef(NoPrefix, newTypeName("C"), List()) + [3] TypeRef(NoPrefix, TypeName("C"), List()) [4] TypeRef(ThisType(java.lang), java.lang.Object, List()) [5] MethodType(List(), TypeRef(ThisType(java.lang), java.lang.Object, List())) - [6] SuperType(ThisType(newTypeName("C")), TypeRef(... java.lang.Object ...)) + [6] SuperType(ThisType(TypeName("C")), TypeRef(... java.lang.Object ...)) [7] TypeRef(ThisType(scala), scala.Int, List()) [8] ConstantType(Constant(2)) @@ -362,10 +362,10 @@ Java の列挙要素への参照はシンボル (`scala.reflect.api.Symbols#Symb scala> showRaw(tpe) res1: String = RefinedType( - List(TypeRef(ThisType(scala), newTypeName("AnyRef"), List())), + List(TypeRef(ThisType(scala), TypeName("AnyRef"), List())), Scope( - newTermName("x"), - newTermName("y"))) + TermName("x"), + TermName("y"))) この `showRaw` メソッドにはデフォルトでは `false` になっている名前付きパラメータ `printIds` と `printKinds` を持つ。 @@ -374,10 +374,10 @@ Java の列挙要素への参照はシンボル (`scala.reflect.api.Symbols#Symb scala> showRaw(tpe, printIds = true, printKinds = true) res2: String = RefinedType( - List(TypeRef(ThisType(scala#2043#PK), newTypeName("AnyRef")#691#TPE, List())), + List(TypeRef(ThisType(scala#2043#PK), TypeName("AnyRef")#691#TPE, List())), Scope( - newTermName("x")#2540#METH, - newTermName("y")#2541#GET)) + TermName("x")#2540#METH, + TermName("y")#2541#GET)) ## 位置情報 diff --git a/ja/overviews/reflection/environment-universes-mirrors.md b/ja/overviews/reflection/environment-universes-mirrors.md index 45bcfaf81a..c8e26b58b8 100644 --- a/ja/overviews/reflection/environment-universes-mirrors.md +++ b/ja/overviews/reflection/environment-universes-mirrors.md @@ -88,7 +88,7 @@ invoker ミラーを作成することができる。 `MethodMirror` はインスタンス・メソッド (Scala にはインスタンス・メソッドのみがある。オブジェクトのメソッドは `ModuleMirror.instance` から取得されるオブジェクト・インスタンスのインスタンス・メソッドだ。) の呼び出しに使われる。作り方: `val mm = im.reflectMethod()`。 具体例: - scala> val methodX = ru.typeOf[C].declaration(ru.newTermName("x")).asMethod + scala> val methodX = ru.typeOf[C].declaration(ru.TermName("x")).asMethod methodX: scala.reflect.runtime.universe.MethodSymbol = method x scala> val mm = im.reflectMethod(methodX) @@ -109,7 +109,7 @@ invoker ミラーを作成することができる。 scala> val im = m.reflect(new C) im: scala.reflect.runtime.universe.InstanceMirror = instance mirror for C@5f0c8ac1 - scala> val fieldX = ru.typeOf[C].declaration(ru.newTermName("x")).asTerm.accessed.asTerm + scala> val fieldX = ru.typeOf[C].declaration(ru.TermName("x")).asTerm.accessed.asTerm fieldX: scala.reflect.runtime.universe.TermSymbol = value x scala> val fmX = im.reflectField(fieldX) @@ -120,7 +120,7 @@ invoker ミラーを作成することができる。 scala> fmX.set(3) - scala> val fieldY = ru.typeOf[C].declaration(ru.newTermName("y")).asTerm.accessed.asTerm + scala> val fieldY = ru.typeOf[C].declaration(ru.TermName("y")).asTerm.accessed.asTerm fieldY: scala.reflect.runtime.universe.TermSymbol = variable y scala> val fmY = im.reflectField(fieldY) diff --git a/ja/overviews/reflection/overview.md b/ja/overviews/reflection/overview.md index c0b6137c54..495be19824 100644 --- a/ja/overviews/reflection/overview.md +++ b/ja/overviews/reflection/overview.md @@ -148,7 +148,7 @@ Scala コンパイラが持つ型情報を全ては入手できない可能性 `shipped` メンバにアクセスするには、前の例と同じく、`p` のクラス (`Purchase`) を含むクラスローダが読み込んだ全てのクラスを入手可能とするミラー `m` を取得することから始める。 - scala> val shippingTermSymb = ru.typeOf[Purchase].declaration(ru.newTermName("shipped")).asTerm + scala> val shippingTermSymb = ru.typeOf[Purchase].declaration(ru.TermName("shipped")).asTerm shippingTermSymb: scala.reflect.runtime.universe.TermSymbol = method shipped 次に、`shipped` フィールドの宣言を照会して `TermSymbol` (`Symbol` 型の 1つ) を得る。 diff --git a/ja/overviews/reflection/symbols-trees-types.md b/ja/overviews/reflection/symbols-trees-types.md index b3995121a0..86e9a69ec2 100644 --- a/ja/overviews/reflection/symbols-trees-types.md +++ b/ja/overviews/reflection/symbols-trees-types.md @@ -70,7 +70,7 @@ title: シンボル、構文木、型 scala> class C[T] { def test[U](x: T)(y: U): Int = ??? } defined class C - scala> val testMember = typeOf[C[Int]].member(newTermName("test")) + scala> val testMember = typeOf[C[Int]].member(TermName("test")) testMember: scala.reflect.runtime.universe.Symbol = method test この場合、`member` は期待される `MethodSymbol` ではなく `Symbol` のインスタンスを返す。 @@ -386,7 +386,7 @@ Scala リフレクションは、ユニバース経由で構文木を視覚化 scala> import scala.reflect.runtime.universe._ import scala.reflect.runtime.universe._ - scala> val tree = Apply(Select(Ident(newTermName("x")), newTermName("$plus")), List(Literal(Constant(2)))) + scala> val tree = Apply(Select(Ident(TermName("x")), TermName("$plus")), List(Literal(Constant(2)))) tree: scala.reflect.runtime.universe.Apply = x.$plus(2) `show` メソッド (もしくは同等の `toString`) を使ってこの構文木が何を表しているかを見てみよう。 @@ -426,7 +426,7 @@ Scala リフレクションは、ユニバース経由で構文木を視覚化 生の構文木の内部構造をインスペクトするには以下のように行う: scala> showRaw(tree) - res1: String = Block(List(ClassDef(Modifiers(), newTypeName("Flower"), List(), Template(List(Ident(newTypeName("AnyRef"))), emptyValDef, List(DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(())))), DefDef(Modifiers(), newTermName("name"), List(), List(), TypeTree(), Literal(Constant("Rose"))))))), Literal(Constant(()))) + res1: String = Block(List(ClassDef(Modifiers(), TypeName("Flower"), List(), Template(List(Ident(TypeName("AnyRef"))), emptyValDef, List(DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(())))), DefDef(Modifiers(), TermName("name"), List(), List(), TypeTree(), Literal(Constant("Rose"))))))), Literal(Constant(()))) ### 構文木の走査 @@ -447,7 +447,7 @@ Scala リフレクションは、ユニバース経由で構文木を視覚化 scala> import scala.reflect.runtime.universe._ import scala.reflect.runtime.universe._ - scala> val tree = Apply(Select(Ident(newTermName("x")), newTermName("$plus")), List(Literal(Constant(2)))) + scala> val tree = Apply(Select(Ident(TermName("x")), TermName("$plus")), List(Literal(Constant(2)))) tree: scala.reflect.runtime.universe.Apply = x.$plus(2) `tree` に対してマッチをかけてやるだけでよく、`Apply` ケースの場合には `Apply` の関数と引数を返す: @@ -468,7 +468,7 @@ Scala リフレクションは、ユニバース経由で構文木を視覚化 は普通かなり複雑となることに注意してほしい。これを示す簡単な例として、上記の構文木に 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)))) + scala> val tree = Apply(Select(Apply(Select(Ident(TermName("x")), TermName("$plus")), List(Literal(Constant(2)))), TermName("$plus")), List(Literal(Constant(3)))) tree: scala.reflect.runtime.universe.Apply = x.$plus(2).$plus(3) これに上記と同じパターンマッチを適用すると外側の `Apply` @@ -479,7 +479,7 @@ Scala リフレクションは、ユニバース経由で構文木を視覚化 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")) + res3: String = Select(Apply(Select(Ident(TermName("x")), TermName("$plus")), List(Literal(Constant(2)))), TermName("$plus")) 特定のノードで止まることなく構文木全体を走査したり、特定の型のノードを収集してインスペクトするなどより複雑なタスクを行うためには `Traverser` を用いた走査の方が適しているかもしれない。 @@ -498,7 +498,7 @@ Scala リフレクションは、ユニバース経由で構文木を視覚化 scala> import scala.reflect.runtime.universe._ 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)))) + scala> val tree = Apply(Select(Apply(Select(Ident(TermName("x")), TermName("$plus")), List(Literal(Constant(2)))), TermName("$plus")), List(Literal(Constant(3)))) tree: scala.reflect.runtime.universe.Apply = x.$plus(2).$plus(3) scala> object traverser extends Traverser { @@ -560,7 +560,7 @@ Scala リフレクションでは、`reify` メソッドを用いた構文木の import scala.reflect.runtime.universe._ scala> { val tree = reify(println(2)).tree; showRaw(tree) } - res0: String = Apply(Select(Select(This(newTypeName("scala")), newTermName("Predef")), newTermName("println")), List(Literal(Constant(2)))) + res0: String = Apply(Select(Select(This(TypeName("scala")), TermName("Predef")), TermName("println")), List(Literal(Constant(2)))) ここで、単に `println(2)` という呼び出しを `reify` している。 つまり、`println(2)` という式をそれに対応する構文木の表現に変換している。そして、生の構文木の内部構造を出力している。 @@ -623,7 +623,7 @@ Scala リフレクションでは、`reify` メソッドを用いた構文木の 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)))) + res2: String = Apply(Ident(TermName("println")), List(Literal(Constant(2)))) `reify` と違って、ツールボックスは型付けの要求を必要としないことに注目してほしい。 この柔軟性の引き換えに堅牢性が犠牲になっている。どういう事かと言うと、`reify` @@ -687,7 +687,7 @@ Scala リフレクションでは、`reify` メソッドを用いた構文木の `println(2)` を使った例題を手動で構築すると、こうなる: - scala> Apply(Ident(newTermName("println")), List(Literal(Constant(2)))) + scala> Apply(Ident(TermName("println")), List(Literal(Constant(2)))) res0: scala.reflect.runtime.universe.Apply = println(2) このテクニックの典型的なユースケースは単独では意味を成さない動的に構築された部分木を組み合わせて構文木を作る必要がある場合だ。 diff --git a/overviews/macros/overview.md b/overviews/macros/overview.md index b984cb9cff..bb216cd336 100644 --- a/overviews/macros/overview.md +++ b/overviews/macros/overview.md @@ -49,7 +49,7 @@ In this document, `<[ expr ]>` denotes the abstract syntax tree that represents Literal(Constant("limit exceeded")) Apply( - Select(Ident(newTermName("x")), newTermName("$less"), + Select(Ident(TermName("x")), TermName("$less"), List(Literal(Constant(10))))) Here is a possible implementation of the `assert` macro: @@ -141,7 +141,7 @@ Note the call to `reify`, which provides a shortcut for creating ASTs. val evals = ListBuffer[ValDef]() def precompute(value: Tree, tpe: Type): Ident = { - val freshName = newTermName(c.fresh("eval$")) + val freshName = TermName(c.fresh("eval$")) evals += ValDef(Modifiers(), freshName, TypeTree(tpe), value) Ident(freshName) } @@ -172,7 +172,7 @@ To follow the example, create an empty directory and copy the code to a new file val evals = ListBuffer[ValDef]() def precompute(value: Tree, tpe: Type): Ident = { - val freshName = newTermName(c.fresh("eval$")) + val freshName = TermName(c.fresh("eval$")) evals += ValDef(Modifiers(), freshName, TypeTree(tpe), value) Ident(freshName) } @@ -248,15 +248,15 @@ With `-Ymacro-debug-lite` it is possible to see both pseudo-Scala representation () } Block(List( - ValDef(Modifiers(), newTermName("eval$1"), TypeTree().setType(String), Literal(Constant("world"))), + ValDef(Modifiers(), TermName("eval$1"), TypeTree().setType(String), Literal(Constant("world"))), Apply( - Select(Select(This(newTypeName("scala")), newTermName("Predef")), newTermName("print")), + Select(Select(This(TypeName("scala")), TermName("Predef")), TermName("print")), List(Literal(Constant("hello")))), Apply( - Select(Select(This(newTypeName("scala")), newTermName("Predef")), newTermName("print")), - List(Ident(newTermName("eval$1")))), + Select(Select(This(TypeName("scala")), TermName("Predef")), TermName("print")), + List(Ident(TermName("eval$1")))), Apply( - Select(Select(This(newTypeName("scala")), newTermName("Predef")), newTermName("print")), + Select(Select(This(TypeName("scala")), TermName("Predef")), TermName("print")), List(Literal(Constant("!"))))), Literal(Constant(()))) diff --git a/overviews/macros/typemacros.md b/overviews/macros/typemacros.md index 8111d47ab3..d272e22654 100644 --- a/overviews/macros/typemacros.md +++ b/overviews/macros/typemacros.md @@ -83,7 +83,7 @@ In Scala programs type macros can appear in one of five possible roles: type rol To put it in a nutshell, expansion of a type macro replace the usage of a type macro with a tree it returns. To find out whether an expansion makes sense, mentally replace some usage of a macro with its expansion and check whether the resulting program is correct. -For example, a type macro used as `TM(2)(3)` in `class C extends TM(2)(3)` can expand into `Apply(Ident(newTypeName("B")), List(Literal(Constant(2))))`, because that would result in `class C extends B(2)`. However the same expansion wouldn't make sense if `TM(2)(3)` was used as a type in `def x: TM(2)(3) = ???`, because `def x: B(2) = ???` (given that `B` itself is not a type macro; if it is, it will be recursively expanded and the result of the expansion will determine validity of the program). +For example, a type macro used as `TM(2)(3)` in `class C extends TM(2)(3)` can expand into `Apply(Ident(TypeName("B")), List(Literal(Constant(2))))`, because that would result in `class C extends B(2)`. However the same expansion wouldn't make sense if `TM(2)(3)` was used as a type in `def x: TM(2)(3) = ???`, because `def x: B(2) = ???` (given that `B` itself is not a type macro; if it is, it will be recursively expanded and the result of the expansion will determine validity of the program). ## Tips and tricks diff --git a/overviews/reflection/annotations-names-scopes.md b/overviews/reflection/annotations-names-scopes.md index 4fdffe525e..007e4d9621 100644 --- a/overviews/reflection/annotations-names-scopes.md +++ b/overviews/reflection/annotations-names-scopes.md @@ -69,7 +69,7 @@ Names are associated with a universe. Example: scala> import scala.reflect.runtime.universe._ import scala.reflect.runtime.universe._ - scala> val mapName = newTermName("map") + scala> val mapName = TermName("map") mapName: scala.reflect.runtime.universe.TermName = map Above, we're creating a `Name` associated with the runtime reflection universe @@ -86,7 +86,7 @@ the `map` method (which is a term) declared in the `List` class, one can do: 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 +`TypeName` instead. It is also possible to rely on implicit conversions to convert between strings and term or type names: scala> listTpe.member("map": TermName) @@ -155,17 +155,17 @@ Trees that accept modifiers are: For example, to create a class named `C` one would write something like: - ClassDef(Modifiers(NoFlags), newTypeName("C"), Nil, ...) + ClassDef(Modifiers(NoFlags), TypeName("C"), Nil, ...) Here, the flag set is empty. To make `C` private, one would write something like: - ClassDef(Modifiers(PRIVATE), newTypeName("C"), Nil, ...) + ClassDef(Modifiers(PRIVATE), TypeName("C"), Nil, ...) Flags can also be combined with the vertical bar operator (`|`). For example, a private final class is written something like: - ClassDef(Modifiers(PRIVATE | FINAL), newTypeName("C"), Nil, ...) + ClassDef(Modifiers(PRIVATE | FINAL), TypeName("C"), Nil, ...) The list of all available flags is defined in `scala.reflect.api.FlagSets#FlagValues`, available via @@ -262,7 +262,7 @@ Example: object Test extends App { val jann = typeOf[JavaAnnottee].typeSymbol.annotations(0).javaArgs - def jarg(name: String) = jann(newTermName(name)) match { + def jarg(name: String) = jann(TermName(name)) match { // Constant is always wrapped in a Literal or LiteralArgument tree node case LiteralArgument(ct: Constant) => value case _ => sys.error("Not a constant") @@ -330,15 +330,15 @@ valid Scala code. scala> showRaw(tree) res1: String = Block(List( - ClassDef(Modifiers(FINAL), newTypeName("C"), List(), Template( - List(Ident(newTypeName("AnyRef"))), + ClassDef(Modifiers(FINAL), TypeName("C"), List(), Template( + List(Ident(TypeName("AnyRef"))), emptyValDef, List( DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List( Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(())))), - DefDef(Modifiers(), newTermName("x"), List(), List(), TypeTree(), + DefDef(Modifiers(), TermName("x"), List(), List(), TypeTree(), Literal(Constant(2))))))), Literal(Constant(()))) @@ -352,23 +352,23 @@ The method `showRaw` can also print `scala.reflect.api.Types` next to the artifa scala> showRaw(cm.mkToolBox().typeCheck(tree), printTypes = true) res2: String = Block[1](List( - ClassDef[2](Modifiers(FINAL), newTypeName("C"), List(), Template[3]( - List(Ident[4](newTypeName("AnyRef"))), + ClassDef[2](Modifiers(FINAL), TypeName("C"), List(), Template[3]( + List(Ident[4](TypeName("AnyRef"))), emptyValDef, List( DefDef[2](Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree[3](), Block[1](List( - Apply[4](Select[5](Super[6](This[3](newTypeName("C")), tpnme.EMPTY), ...))), + Apply[4](Select[5](Super[6](This[3](TypeName("C")), tpnme.EMPTY), ...))), Literal[1](Constant(())))), - DefDef[2](Modifiers(), newTermName("x"), List(), List(), TypeTree[7](), + DefDef[2](Modifiers(), TermName("x"), List(), List(), TypeTree[7](), Literal[8](Constant(2))))))), Literal[1](Constant(()))) [1] TypeRef(ThisType(scala), scala.Unit, List()) [2] NoType - [3] TypeRef(NoPrefix, newTypeName("C"), List()) + [3] TypeRef(NoPrefix, TypeName("C"), List()) [4] TypeRef(ThisType(java.lang), java.lang.Object, List()) [5] MethodType(List(), TypeRef(ThisType(java.lang), java.lang.Object, List())) - [6] SuperType(ThisType(newTypeName("C")), TypeRef(... java.lang.Object ...)) + [6] SuperType(ThisType(TypeName("C")), TypeRef(... java.lang.Object ...)) [7] TypeRef(ThisType(scala), scala.Int, List()) [8] ConstantType(Constant(2)) @@ -391,10 +391,10 @@ on by the Scala typechecker. scala> showRaw(tpe) res1: String = RefinedType( - List(TypeRef(ThisType(scala), newTypeName("AnyRef"), List())), + List(TypeRef(ThisType(scala), TypeName("AnyRef"), List())), Scope( - newTermName("x"), - newTermName("y"))) + TermName("x"), + TermName("y"))) The `showRaw` method also has named parameters `printIds` and `printKinds`, both with default argument `false`. When passing `true` to these, `showRaw` @@ -403,10 +403,10 @@ additionally shows the unique identifiers of symbols, as well as their kind scala> showRaw(tpe, printIds = true, printKinds = true) res2: String = RefinedType( - List(TypeRef(ThisType(scala#2043#PK), newTypeName("AnyRef")#691#TPE, List())), + List(TypeRef(ThisType(scala#2043#PK), TypeName("AnyRef")#691#TPE, List())), Scope( - newTermName("x")#2540#METH, - newTermName("y")#2541#GET)) + TermName("x")#2540#METH, + TermName("y")#2541#GET)) ## Positions diff --git a/overviews/reflection/environment-universes-mirrors.md b/overviews/reflection/environment-universes-mirrors.md index 083bd92951..afe3101f0e 100644 --- a/overviews/reflection/environment-universes-mirrors.md +++ b/overviews/reflection/environment-universes-mirrors.md @@ -91,7 +91,7 @@ An `InstanceMirror` is used for creating invoker mirrors for methods and fields 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 + scala> val methodX = ru.typeOf[C].declaration(ru.TermName("x")).asMethod methodX: scala.reflect.runtime.universe.MethodSymbol = method x scala> val mm = im.reflectMethod(methodX) @@ -111,7 +111,7 @@ A `FieldMirror` is used for getting/setting instance fields (like methods, Scala scala> val im = m.reflect(new C) im: scala.reflect.runtime.universe.InstanceMirror = instance mirror for C@5f0c8ac1 - scala> val fieldX = ru.typeOf[C].declaration(ru.newTermName("x")).asTerm.accessed.asTerm + scala> val fieldX = ru.typeOf[C].declaration(ru.TermName("x")).asTerm.accessed.asTerm fieldX: scala.reflect.runtime.universe.TermSymbol = value x scala> val fmX = im.reflectField(fieldX) @@ -122,7 +122,7 @@ 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 + scala> val fieldY = ru.typeOf[C].declaration(ru.TermName("y")).asTerm.accessed.asTerm fieldY: scala.reflect.runtime.universe.TermSymbol = variable y scala> val fmY = im.reflectField(fieldY) diff --git a/overviews/reflection/overview.md b/overviews/reflection/overview.md index 8f068d43dd..f533f45cab 100644 --- a/overviews/reflection/overview.md +++ b/overviews/reflection/overview.md @@ -168,7 +168,7 @@ which makes all classes and types available that are loaded by the classloader 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 + scala> val shippingTermSymb = ru.typeOf[Purchase].declaration(ru.TermName("shipped")).asTerm shippingTermSymb: scala.reflect.runtime.universe.TermSymbol = method shipped We now look up the declaration of the `shipped` field, which gives us a diff --git a/overviews/reflection/symbols-trees-types.md b/overviews/reflection/symbols-trees-types.md index e0cd5054ac..437cfae0c9 100644 --- a/overviews/reflection/symbols-trees-types.md +++ b/overviews/reflection/symbols-trees-types.md @@ -81,7 +81,7 @@ For example, scala> class C[T] { def test[U](x: T)(y: U): Int = ??? } defined class C - scala> val testMember = typeOf[C[Int]].member(newTermName("test")) + scala> val testMember = typeOf[C[Int]].member(TermName("test")) testMember: scala.reflect.runtime.universe.Symbol = method test In this case, `member` returns an instance of `Symbol`, not `MethodSymbol` as @@ -417,7 +417,7 @@ For example, given the following tree: scala> import scala.reflect.runtime.universe._ import scala.reflect.runtime.universe._ - scala> val tree = Apply(Select(Ident(newTermName("x")), newTermName("$plus")), List(Literal(Constant(2)))) + scala> val tree = Apply(Select(Ident(TermName("x")), TermName("$plus")), List(Literal(Constant(2)))) tree: scala.reflect.runtime.universe.Apply = x.$plus(2) We can use method `show` (or `toString`, which is equivalent) to see what that @@ -461,7 +461,7 @@ the tree that `expr` contains by: And we can inspect the raw tree by simply doing: scala> showRaw(tree) - res1: String = Block(List(ClassDef(Modifiers(), newTypeName("Flower"), List(), Template(List(Ident(newTypeName("AnyRef"))), emptyValDef, List(DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(())))), DefDef(Modifiers(), newTermName("name"), List(), List(), TypeTree(), Literal(Constant("Rose"))))))), Literal(Constant(()))) + res1: String = Block(List(ClassDef(Modifiers(), TypeName("Flower"), List(), Template(List(Ident(TypeName("AnyRef"))), emptyValDef, List(DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(())))), DefDef(Modifiers(), TermName("name"), List(), List(), TypeTree(), Literal(Constant("Rose"))))))), Literal(Constant(()))) ### Traversing Trees @@ -483,7 +483,7 @@ in the following tree: scala> import scala.reflect.runtime.universe._ import scala.reflect.runtime.universe._ - scala> val tree = Apply(Select(Ident(newTermName("x")), newTermName("$plus")), List(Literal(Constant(2)))) + scala> val tree = Apply(Select(Ident(TermName("x")), TermName("$plus")), List(Literal(Constant(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` @@ -507,7 +507,7 @@ arbitrarily deep within other nodes. A simple illustration would be if we were 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)))) + scala> val tree = Apply(Select(Apply(Select(Ident(TermName("x")), TermName("$plus")), List(Literal(Constant(2)))), TermName("$plus")), List(Literal(Constant(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 @@ -519,7 +519,7 @@ we saw above: 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")) + res3: String = Select(Apply(Select(Ident(TermName("x")), TermName("$plus")), List(Literal(Constant(2)))), TermName("$plus")) In cases where one must do some richer task, such as traversing an entire tree without stopping at a specific node, or collecting and inspecting all @@ -545,7 +545,7 @@ all `Apply` nodes, we could do: scala> import scala.reflect.runtime.universe._ 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)))) + scala> val tree = Apply(Select(Apply(Select(Ident(TermName("x")), TermName("$plus")), List(Literal(Constant(2)))), TermName("$plus")), List(Literal(Constant(3)))) tree: scala.reflect.runtime.universe.Apply = x.$plus(2).$plus(3) scala> object traverser extends Traverser { @@ -615,7 +615,7 @@ Scala Reflection. To see why, let's start with a small example: import scala.reflect.runtime.universe._ scala> { val tree = reify(println(2)).tree; showRaw(tree) } - res0: String = Apply(Select(Select(This(newTypeName("scala")), newTermName("Predef")), newTermName("println")), List(Literal(Constant(2)))) + res0: String = Apply(Select(Select(This(TypeName("scala")), TermName("Predef")), TermName("println")), List(Literal(Constant(2)))) Here, we simply `reify` the call to `println(2)`-- that is, we convert the expression `println(2)` to its corresponding tree representation. Then we @@ -690,7 +690,7 @@ section: 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)))) + res2: String = Apply(Ident(TermName("println")), List(Literal(Constant(2)))) It's important to note that, unlike `reify`, toolboxes aren't limited by the typeability requirement-- although this flexibility is achieved by sacrificing @@ -765,7 +765,7 @@ and fragility. Our earlier example involving `println(2)` can be manually constructed as follows: - scala> Apply(Ident(newTermName("println")), List(Literal(Constant(2)))) + scala> Apply(Ident(TermName("println")), List(Literal(Constant(2)))) res0: scala.reflect.runtime.universe.Apply = println(2) The canonical use case for this technique is when the target tree needs to be