8000 Update deprecated constructs in code example newTermName -> TermName newTypeName -> TypeName by at15 · Pull Request #425 · scala/docs.scala-lang · GitHub
[go: up one dir, main page]

Skip to content

Update deprecated constructs in code example newTermName -> TermName newTypeName -> TypeName #425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions ja/overviews/macros/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` マクロの実装の一例を載せる:
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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(())))

Expand Down
2 changes: 1 addition & 1 deletion ja/overviews/macros/typemacros.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` そのものが型マクロではないとする。その場合は再帰的に展開され、その展開の結果がプログラムの妥当性を決定する。)

## コツとトリック

Expand Down
42 changes: 21 additions & 21 deletions ja/overviews/reflection/annotations-names-scopes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` を作成している。
Expand All @@ -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)
Expand Down Expand Up @@ -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` にて定義されており、
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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(())))

Expand All @@ -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))

Expand All @@ -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` を持つ。
Expand All @@ -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))

## 位置情報

Expand Down
6 changes: 3 additions & 3 deletions ja/overviews/reflection/environment-universes-mirrors.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ invoker ミラーを作成することができる。
`MethodMirror` はインスタンス・メソッド (Scala にはインスタンス・メソッドのみがある。オブジェクトのメソッドは `ModuleMirror.instance` から取得されるオブジェクト・インスタンスのインスタンス・メソッドだ。) の呼び出しに使われる。作り方: `val mm = im.reflectMethod(<method symbol>)`。
具体例:

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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion ja/overviews/reflection/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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つ) を得る。
Expand Down
20 changes: 10 additions & 10 deletions ja/overviews/reflection/symbols-trees-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` のインスタンスを返す。
Expand Down Expand Up @@ -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`) を使ってこの構文木が何を表しているかを見てみよう。
Expand Down Expand Up @@ -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(())))

### 構文木の走査

Expand All @@ -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` の関数と引数を返す:
Expand All @@ -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`
Expand All @@ -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` を用いた走査の方が適しているかもしれない。
Expand All @@ -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 {
Expand Down Expand Up @@ -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)` という式をそれに対応する構文木の表現に変換している。そして、生の構文木の内部構造を出力している。
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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)

このテクニックの典型的なユースケースは単独では意味を成さない動的に構築された部分木を組み合わせて構文木を作る必要がある場合だ。
Expand Down
Loading
0