scala.Predef
Docs: Scala Standard Library 2.13.1 - scala.Predef
Pre = 事前
Def(ine) = 定義
ってことっぽい
scala.Predef
はすべてのScalaソースにおいて自動import
static importみたいな感じか?おなじみのprint
, println
もscala.Predef
で定義されている
* === Console Output === * For basic console output, `Predef` provides convenience methods [[print(x:Any* print]] and [[println(x:Any* println]], * which are aliases of the methods in the object [[scala.Console]].
こいつら
object Predef extends LowPriorityImplicits with DeprecatedPredef { ... /** Prints an object to `out` using its `toString` method. * * @param x the object to print; may be null. * @group console-output */ def print(x: Any) = Console.print(x) /** Prints a newline character on the default output. * @group console-output */ def println() = Console.println()
JavaではSystem.out.print
と呼んでいたものが、Scalaではprint
のみで呼べるのはPredef
のおかげ
またMap
, Set
などもつかえる
こいつらについてはscalaパッケージ配下にいないが、Predef
でvalしてるので呼び出せる。
↓それっぽいことがかいてある
* === Commonly Used Types === * Predef provides type aliases for types which are commonly used, such as * the immutable collection types [[scala.collection.immutable.Map]], * [[scala.collection.immutable.Set]], and the [[scala.collection.immutable.List]] * constructors ([[scala.collection.immutable.::]] and * [[scala.collection.immutable.Nil]]).
// miscellaneous ----------------------------------------------------- scala.`package` // to force scala package object to be seen. scala.collection.immutable.List // to force Nil, :: to be seen. /** @group aliases */ type Function[-A, +B] = Function1[A, B] /** @group aliases */ type Map[A, +B] = immutable.Map[A, B] /** @group aliases */ type Set[A] = immutable.Set[A] /** @group aliases */ val Map = immutable.Map /** @group aliases */ val Set = immutable.Set
タプルと
@deprecated("use built-in tuple syntax or Tuple2 instead", "2.11.0") type Pair[+A, +B] = Tuple2[A, B] @deprecated("use built-in tuple syntax or Tuple2 instead", "2.11.0") object Pair { def apply[A, B](x: A, y: B) = Tuple2(x, y) def unapply[A, B](x: Tuple2[A, B]): Option[Tuple2[A, B]] = Some(x) } @deprecated("use built-in tuple syntax or Tuple3 instead", "2.11.0") type Triple[+A, +B, +C] = Tuple3[A, B, C] @deprecated("use built-in tuple syntax or Tuple3 instead", "2.11.0") object Triple { def apply[A, B, C](x: A, y: B, z: C) = Tuple3(x, y, z) def unapply[A, B, C](x: Tuple3[A, B, C]): Option[Tuple3[A, B, C]] = Some(x) }
アロー
/** @group implicit-classes-any */ implicit final class ArrowAssoc[A](private val self: A) extends AnyVal { @inline def -> [B](y: B): Tuple2[A, B] = Tuple2(self, y) def →[B](y: B): Tuple2[A, B] = ->(y) }
アローもここにいる
よく見るのはMapのときとか
scala> val im = Map("a" -> "A") im: scala.collection.immutable.Map[String,String] = Map(a -> A)
っていうのとかいろいろなのがscala.Predef
に定義、自動でインポートされているからユーザーは違和感なくつかえるんだねということ
ここででてきた->
, Tuple
, Map
の関連性について次でもうちょっとくわしく書くよ