Scala 3ã«ã¯å¤§ããªæ§æä¸ã®å¤æ´ãä¾ãã°implicitã¾ãããæ´çããã¦givenãªã©ã«å¤åããããã¦ããä¸æ¹ã§ãç´°ãããªæ©å¾®ã«ãããå¤æ´ãå«ã¾ãã¦ãããä»å説æããimportæ§æä¸ã®ã¯ã¤ã«ãã«ã¼ãè¨å·ã®å¤æ´ããã®ä¸é¨ã
Scala 2ã§ã¯ãããããã±ã¼ã¸ã«å±ãããã®ãå
¨ã¦ã¤ã³ãã¼ããã¦ããã«ã¯ä»¥ä¸ã®ããã«_
ãã¯ã¤ã«ãã«ã¼ãã¨ãã¦ç¨ãã¦ãã:
import java.util._
ä¸æ¹ãScala 3ã§ã¯*
ãã¯ã¤ã«ãã«ã¼ãã¨ãã¦ä½¿ã:
import java.util.*
ããããããã¨ã«æ§æ¥ã®_
ã äºææ§ããµãã¼ãããã¦ããããã§ä½¿ããã¨ãã§ããããåºæ¬çã«ã¯*
ã使ããã¨ãæ¨å¥¨ãããã
ãªã_
ã§ã¯ãªã*
ãªã®ã
Scala 3ã§ã¯ã¤ã«ãã«ã¼ãè¨å·ãå¤æ´ãããèæ¯ã«ã¯ã_
ã®ã»ãã³ãã£ãã¯ã®æ··ä¹±ããããScalaã§ã¯å¾æ¥ãããããã±ã¼ã¸ã®ä¸é¨ãimportããªãã¨ããæ©è½(ããããshadowing)ãç¨æããã¦ãã:
// Scala 2 import java.util.{ Random => _, _ }
ä¸æ²ã®ä¾ã§ã¯Random
ã¯importãããããã以å¤ã®å
¨ã¦ã®ã·ã³ãã«ãimportãããåã®è¯ãèªè
ãªãã°ãæ°ä»ãã ããããããã§ã»ãã³ãã£ãã¯ã®ç«¶åãèµ·ãã£ã¦ãã¾ã£ã¦ããã
Random
ã®importãé²ããã¤ã¾ãå¿åã«åãã¾ãããã¨ããæå³ã§ã®_
java.util
ã«æå±ããå ¨ã¦ã®ã·ã³ãã«ã¨ããæå³ã§ã®_
ãã®ããã«ã_
ã«ã¯å¿åã¨å
¨ã¦ã¨ããå¥ã
ã®æå³ãä¸ãããã¦ãããScala 3以éã¯ãã®ææ§ããæé¤ããå¥ã
ã®è¨å·ãä¸ãããã¨ã§ã»ãã³ãã£ãã¯ã以ä¸ã®ããã«æ確åããã
- å¿åã¨ããæå³ã§
_
ãç¨ãã - å
¨ã¦ã®ã¨ããæå³ã§
*
ãç¨ãã
ããã«ãããScala 3ã§ã¯shadowingã以ä¸ã®ããã«è¡¨ç¾ã§ããããã«ãªã£ã:
// Scala 3 import java.util.{ Random as _, * } // =>ã§ã¯ãªãasã使ãããã«ãªã£ã
Random
ã¯importããã«ããã®ä»ã®å
¨ã¦ã®ã·ã³ãã«ãimportããã¨ããã»ãã³ãã£ãã¯ãããä¸å±¤æ確ã«ãªã£ã¦ãããã¨ããããã
è£è¶³
åãããªãã¨ãæ¸ãã¦ããããæ ¹æ ã¨ãªãæ å ±ããªããDottyã®Issueãæ¢ãã¦ãè¦å½ããªãã®ã§ãå®ã¯å ¨ç¶éãã¢ããã¼ã·ã§ã³ã§å¤æ´ãããã®ãããããªãããä»ã«ã¢ããã¼ã·ã§ã³ãè¦å½ããªãã®ã§ãã»ãã³ãã£ãã¯ç«¶åãé²ãããã«ãããªã£ããã¨ãã説æããã¦ããã
ãã¾ã: åã«ãããã¯ã¤ã«ãã«ã¼ãè¨å·ã®å¤æ´
ã¨ããã§ããã®ãããªã»ãã³ãã£ãã¯æ確åã®ããã®è¨å·ã®ä¿®æ£ã¯åãã©ã¡ã¼ã¿ã¾ããã§ãè¡ããã¦ããã
Scala 2ã§ã¯ãF[_]
ã¨æ¸ããã¨ããæèã«ãã£ã¦2ã¤ã®ã»ãã³ãã£ãã¯ã使ãåããå¿
è¦ããã£ã:
- ããããåããã©ã¡ã¼ã¿ã¨ãã¦åãåã
F
ã¨ããæå³ - ãªãã§ãè¯ãããã©ã¡ã¼ã¿ãåãåãå
F
ãããã¨ããæå³
Scala 3ã§ã¯ãåã«å¯¾ããã¯ã¤ã«ãã«ã¼ãã¨ãã¦?
è¨å·ãæ°ãã«å°å
¥ãããããå
¨ã¦ã®ãã¨ããæå³ã®ã¯ã¤ã«ãã«ã¼ãã¯ãã®è¨å·ãæ
ãããã«ãªãã
val lis: List[?] = List(1, 2, 3) // ä¸èº«ã¯ãªãã§ãããList // lis.map(_ + 1) // Intã®æ å ±ã¯æ¨ã¦å»ãããã®ã§ã³ã³ãã¤ã«ããªã def twice[A <: Function0[?]](f: A): Unit = { f(); f() } // è¿ãå¤ã¯ãªãã§ãããFunction0ã§ãããããªAãäºåº¦å¼ã¶ twice(() => println("hello!")) // hello! ã¨äºåº¦è¡¨ç¤ºããã
ãã¬ã¼ã¹ãã«ãã¨ãã¦ã®ãããããåãããã¨ãã»ã»ã»ã¨ããæã«ä½¿ãè¨å·ã¨ãã¦ã¯_
ãå¼ãç¶ã使ãããããä»»æã®ãã¨ããæå³ã¯ãã®è¨å·ãæ
ãããã«ãªãã
import cats.implicits.* import cats.* def left[A, F[_]: Applicative](x: F[A], y: F[A]): F[A] = x <* y println(left(42.some, 84.some)) // => Some(42)
F[_]ãScala 3.2 ã¾ã§ã¯å©ç¨æã¨å®£è¨æã§å ¨ç§°ã¨åå¨ãã²ã£ããè¿ã£ã¦ããã®ãããã©ãã§ããã (å©ç¨æ㯠"âX. F[X]" ãªã®ã«å®£è¨æ㯠"âX. F[X]")
— éåè«ã»åè« (@Kory__3) 2023å¹´2æ22æ¥
Scala 3.2 以é㯠_ 㯠type placeholder ã®æå³ã«çµ±ä¸ãã㦠wildcard ã®æå³ã¯ ? ã«åé¢ãããã¯ã
— éåè«ã»åè« (@Kory__3) 2023å¹´2æ22æ¥