ã¬ãã¬ãã¸ã¡ã¢: Scalaã¹ã±ã¼ã©ãã«ããã°ã©ãã³ã°
è¨èªãä¼½èã¨ãã¶ã¼ã«ã®ããã«æé·ãã--Steele "Growing a language" 1999
ãã¬ã¤ã=ã¤ã³ã¿ã¼ãã§ã¤ã¹ã«ä¼¼ã¦ãããã¡ã½ããå®è£ ããã£ã¼ã«ããæã¤ãããã¯ã¹ã¤ã³åæåºæ¥ã
Javaã®Stringã«toIntããªããã¨ã«æ°ã¥ããã¨ãã«ãæé»ã®åå¤æãåç §ãã¦RichStringã«å¤æãããã®toIntãå¼ã¶
ã¢ã©ã³ã»ã±ã¤ãç§ã¯åã«å対ãã¦ããããã§ã¯ãªãããå®å ¨ãªè¦çãã«ã¯ãªããªããããªåã·ã¹ãã ãè¦ããã¨ããªãã®ã§åçãªçä»ããæ¯æãã¦ããã®ã§ããã2003 Dr. Alan Kay on the Meaning of "Object-Oriented Programming"
ãã¤ã¯ã¹ãã©ããã¹ãã証æã§ããã®ã¯ã¨ã©ã¼ã®åå¨ã§ãã£ã¦ãã¨ã©ã¼ã®ä¸å¨ã§ã¯ãªãã 1970 http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD249.PDF
ããã¨ãªãå¿ç¨åéã¸ã®æ¡å¼µæ§ãæã£ãè¨èªãã¨ããã¢ã¤ãã¢1966 The next 700 Programming Languages http://www.thecorememory.com/Next_700.pdf
(arg: String) => println(arg)ãprintlnã£ã¦æ¸ããã®ããé¢æ°ã®é¨åé©ç¨ãã¨å¼ã¶ã®ã¯æ°æãæªããªãã P.51
æ«å°¾ãã³ãã³ã§ããæ¼ç®åã¯å³ãªãã©ã³ãã®ã¡ã½ããã§ãã
ã³ã³ãããªã³ãªãã¸ã§ã¯ã 4.3
ã¿ãã«ã¨ãªã¹ãã®ä¸¡æ¹ããã£ã¦ä¸¡æ¹ã¤ãã¥ã¼ã¿ãã«ãHaskellã®ã¿ãã«ã¨ãªã¹ãã ã¨æãã°ã ããããã£ã¦ã
ãã£ã¼ã«ãã¨ã¡ã½ãããç·ç§°ãã¦ã¡ã³ãã¼ã¨å¼ã¶ãã¡ã³ãã¼ã¯ããã©ã«ãã§public
varã¨valããããvalã¯åä»£å ¥ä¸å¯ã
Scalaã®ã¯ã©ã¹ã¯éçã¡ã³ã(static)ãæã¦ãªãããã®ãããã«ã·ã³ã°ã«ãã³ãªãã¸ã§ã¯ãããããclassãã¼ã¯ã¼ãã®ä»£ããã«objectãã¼ã¯ã¼ãã使ããã·ã³ã°ã«ãã³ãªãã¸ã§ã¯ããã¯ã©ã¹ã¨åãååã®æã«ã³ã³ãããªã³ãªãã¸ã§ã¯ããã³ã³ãããªã³ã¯ã©ã¹ã¨å¼ã¶ãåãååã®ã¯ã©ã¹ããªãã·ã³ã°ã«ãã³ãªãã¸ã§ã¯ãã¯ã¹ã¿ã³ãã¢ãã³ãªãã¸ã§ã¯ãã
importæã§ä»»æã®ãªãã¸ã§ã¯ãããã¡ã³ãã¼ãã¤ã³ãã¼ãåºæ¥ãã
ãã¹ã¦ã®ã¡ã½ãããæ¼ç®åã«ãªãããs.indexOf('o')ãs indexOf 'o'ã¨æ¸ããã
è«çæ¼ç®åãã¡ã½ããâãããã·ã§ã¼ããµã¼ãããã¯ã©ããã£ã¦ãï¼âå®ã¯å ¨ã¦ã®ã¡ã½ããã§åå渡ããå¯è½(9.5)âãªãã ã£ã¦ï¼ï¼
===åå¼ç¨ãã¨ã«ãã¼ã¸æ°ãæ¸ãã®ã¯é¢åãªã®ã§ããã§P.100===
ã¤ãã¥ã¼ã¿ãã«ãªãªãã¸ã§ã¯ãã®ãã¬ã¼ããªã P.105
é·æï¼è¤éãªç¶æ
å¤åãããªãã®ã§åä½ãæ¨å®ãããããä»ã®ã³ã¼ãã«æ¸¡ãéã«ç ´å£ãé¿ãã¦ã³ãã¼ãã¨ãå¿
è¦ããªããã¹ã¬ãããåæã¢ã¯ã»ã¹ãã¦ãå£ããªããå®å
¨ã«ããã·ã¥ã®ãã¼ã«ã§ãã
çæï¼å¤§è¦æ¨¡ãªãªãã¸ã§ã¯ãã®ã³ãã¼ãå¿
è¦ã«ãªããã¨ããã
P108 ã¯ã©ã¹ãã©ã¡ã¼ã¿ã¯ã³ã³ã¹ãã©ã¯ã¿å ããããã¢ã¯ã»ã¹åºæ¥ãªãï¼âã¯ã©ã¹ãã©ã¡ã¼ã¿ããã£ã¼ã«ãã«ä»£å ¥ãã¦è§£æ±ºâãããªãã®ããã®ã¯ã©ã¹ãã©ã¡ã¼ã¿â¦ 10.6ã®ãã©ã¡ã¼ã¿ãã£ã¼ã«ãã§è§£æ±ºãããï¼ï¼
def this(n: Int) = this(n, 1)ã§ã2çªç®ã®å¼æ°ã«1ãå ¥ãã¦ã³ã³ã¹ãã©ã¯ã¿ãå¼ã¶ãã£ã¦ããè£å©ã³ã³ã¹ãã©ã¯ã¿ãã§ãããåºæ¬ã³ã³ã¹ãã©ã¯ã¿ã ããã¹ã¼ãã¼ã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ãå¼ã³åºãããããã¯Javaããè¥å¹²å¶éãå¼·ããsee sec.10
æ¼ç®åã¡ã½ããã使ãã°ã³ã¼ãã¯ç°¡æ½ã«ãªãããããèªã¿ããããªããã©ããã¯ã¯ã©ã¤ã¢ã³ãããã°ã©ããåã ã®æ¼ç®åã®æå³ãç解ãã¦è¦ãããããã©ããã«ãã P.120
Scalaã§ã¯é¢æ°ãªãã©ã«ã¨ã®ç¸æ§ãæªãbreakã¨continueãåãé¤ãã¦ãããæ«å°¾å帰ããwhileã®æ¡ä»¶ã«toExitçå¤æ°ãå ¥ãããã«ã¼ãã®å¾åãifã§å²ã£ãããããã¨ã§breakãcontinueããªãã¦ãåçãªã³ã¼ããæ¸ããã¨ãã説æãããã¦ãããã¾ãããããããã ãã©ããã©ãç¸æ§ããããã®ãç¥ããããã©ã©ãã«æ¸ãã¦ããã®ãããããªãã
Scalaã¯ãã¹ãããã¹ã³ã¼ãã®å å´ã§å¤å´ã®å¤æ°ãã·ã£ãã¼ãããããªå¤æ°ãå®ç¾©ã§ããã
ãä¸äººåã®åå¨ã¨ãã¦ã®é¢æ°ã(first-class functions)ï¼
ã(x: Int) => x > 0ãã¯ã_ > 0ãã¨ãæ¸ãããã_ + _ãã¯1å¼æ°ã®2åã«ãªãé¢æ°ã§ã¯ãªã2å¼æ°ã®è¶³ãç®ãããé¢æ°
Javaã®å é¨ã¯ã©ã¹ã¯å¤å´ã®ã¹ã³ã¼ãã§æ¸ãæããããå¯è½æ§ã®ããå¤æ°ã«ã¯ã¢ã¯ã»ã¹åºæ¥ãªããScalaã®ã¯ãã¼ã¸ã£ã¯ç´æçã«è¨ãã°å¤æ°äºæ ãæ´ãã§ãéããããã®ã§ããã
Scalaã¯æ«å°¾å帰ãã¸ã£ã³ãã«å¤æããã®ã§ãæ«å°¾å帰ã®é¢æ°ã失æããæã®ã¹ã¿ãã¯ãã¬ã¼ã¹ã«ã¯ããã¤ãã®é¢æ°ãåºã¦ããªããç´ããããæã¯-g:notailcallsãªãã·ã§ã³ã§OFFã«ãããæ«å°¾å帰ã®æé©åã¯æåéãèªåèªèº«ãå¼ã¶å帰ã®æã«ãã使ããªããç¸äºå帰ãä»ã®é¢æ°ã®å¼ã³åºãã¯æé©åãããªãã
def foo(f: () => Boolean) = ...ã§å®ç¾©ãããé¢æ°ã¯ foo(() => x > 0)ã¨å¼ã¹ãããããdef foo(f: => Boolean)ã«ããã¨foo(x > 0)ã¨å¼ã¹ãããã«ãªããfoo(v: Boolean)ã¨ã®éãã¯è©ä¾¡ã®ã¿ã¤ãã³ã°ããããåå渡ããã©ã¡ã¼ã¿ã
åç´ãªãªãã¸ã§ã¯ãã¨ã¯ä½ããåç´ãªãªãã¸ã§ã¯ãããããé¢ç½ããªãã¸ã§ã¯ããä½ãã«ã¯ã©ãããã°ããããã³ã³ããã¼ã¿(åææ¼ç®)ã¯ã©ãçµã¿åããããããæãä¸è¬çãªã³ã³ããã¼ã¿ã¯ä½ãï¼ãããã«çãããããªãã©ã¤ãã©ãªè¨è¨ã¯è»éã«ã®ã£ã¦ããã¨è¨ããã ãããP.173
ãµãã¯ã©ã¹ã¨ãµãåã®éã: åå¼æ°ãåããªãã¯ã©ã¹ã»ãã¬ã¤ãã«é¢ãã¦ã¯ãµãã¯ã©ã¹ã¨ãµãåã¯åããåå¼æ°ãæã¤å ´åãããå ±å¤(ãã¨ãã°Listã¯List[+A]ãªã®ã§List[Cat] <: List[Animal])ãªããµãå ãã©ã¡ã¼ã¿ãã£ã¼ã«ãï¼è¦ããã«class Foo(val arg: Type)ã£ã¦æ¸ãæ¹ã§ãã£ã¼ã«ããã¤ããããclass Foo(override val x)ã¨ãprivateãªã©ã®ä¿®é£¾ãä»ããããã 親ã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã¯åºæ¬ã³ã³ã¹ãã©ã¯ã¿ããããå¼ã¹ãªããclass Foo(...args...) extends SuperClass(...args for superclass...) å ·è±¡ã¡ã³ãã®ãªã¼ãã©ã¤ãã«ã¯override修飾ãå¿ é Scalaã¹ã±ã¼ã©ãã«ããã°ã©ãã³ã°ã®ã¬ãã¬ãã¸ã¡ã¢
å¼ã³åºãããã¡ã½ããã®å®ç¾©ã¯å¤æ°ã®åã§ã¯ãªãå®è¡æã®ãªãã¸ã§ã¯ãã®ã¯ã©ã¹ã«ãã£ã¦æ±ºã¾ããåçæç¸ã
ç¶æ¿ã使ãåã«is-aé¢ä¿ãã©ãããèãã(Mayers, "Effective C++" 1991)ãã¾ããã¹ã¼ãã¼ã¯ã©ã¹åã¨ãã¦ãµãã¯ã©ã¹åã使ããããã©ãããèãã(Eckel, "Thinking in Java" 1998)
æä¸ä½(bottom)ã®å: Null, NothingãNullã¯å ¨ã¦ã®åç §ã¯ã©ã¹(AnyRefã¯ã©ã¹ãç¶æ¿ããã¯ã©ã¹)ã®å°ã¯ã©ã¹ãNothingã¯å ¨ã¦ã®å°ã¯ã©ã¹ããã¨ãã°errorã®è¿ãå¤ãªã©ã失æãããIntãè¿ãé¢æ°ã®åãMaybe Intã¨ãã«ããªããIntãè¿ãã¦ãã¨ã©ã¼æã«ã¯ä¾å¤æ©æ§ã§NothingãIntã¨ãã¦ä½¿ãåã«å¥ã®å¦çã«é£ã°ãããã
P.204 ãã¬ã¤ã ãã¬ã¤ãã¯ã¯ã©ã¹ã¿ãããªãã®ãéãã¯superã®æããã®ãä½ããtraitã®superå¼åºã¯ãããã¯ã©ã¹ã«ããã¯ã¹ã¤ã³ããããã¨ã§ç·å½¢åã«ãã£ã¦æ±ºã¾ãããã®ããtraitã®abstractã¡ã½ããã®ä¸ã§ã¯super.a_methodã«ã¢ã¯ã»ã¹ãããã¨ãã§ãããããã¯a_methodã®å ·ä½çãªå®ç¾©ãæä¾ããã¯ã©ã¹ã®ãã¨ã§mixinããªããã°ãªããªããã¨ã表æãã¦ããã
ãã®ãã¨ã«ããtraitã¨ããå½¢ã§è¡¨ç¾ããããå¤æ´ã®å·®åããç©ã¿éãããã¨ãã§ãã(åã¯å·®åãç©ã¿éããã³ã¼ãã£ã³ã°ã¹ã¿ã¤ã«ããããã®ãã©ããã¯çåã ã)
Scalaã®importã¯ã©ãã«ã§ãæ¸ããããªãã¸ã§ã¯ããã¤ã³ãã¼ãã§ãããã¤ã³ãã¼ããããã¡ã³ãã®ååãå¤ããããã
ã¯ã©ã¹ã¨ã³ã³ãããªã³ãªãã¸ã§ã¯ãã¯ç¸äºã«privateç¡è¦ãããã使ã£ã¦staticã¡ã³ãã®ä»£ããã«ããã
ã±ã¼ã¹ã¯ã©ã¹ã
abstract class Expr case class Var(name: String) extends Expr case class Number(...) extends Expr case ...
ãã¿ã¼ã³ãããã«ä½¿ãã
ScalaãJavaãæ¶å»ã¢ãã«(erasure model)ã®ã¸ã§ããªãã¯ããã°ã©ãã³ã°ãªã®ã§ä¸ããããMapãªãã¸ã§ã¯ããMap
sealedã¯ã©ã¹ã«ããã¨åããã¡ã¤ã«ã§å®ç¾©ããã¦ãããµãã¯ã©ã¹ä»¥å¤ã«ã¯ãµãã¯ã©ã¹ãä½ããã¨ãã§ããªããªãã
åãã©ã¡ã¼ã¿ã¨ãµãåã®çµã¿åããã¯é¢ç½ãåé¡ãã¯ããã§ãããS <: T (SãTãæå¾ ããã¦ããç®æã§åå®å ¨ã«ä½¿ç¨ã§ãã)ã®æãFoo[S] <: Foo[T]ãªãFooã¯å ±å¤(covariant)ã¨ããFoo[+T]ã¨è¡¨ç¾ãããFoo[-T]ãããããã¯åå¤(contravariant)ãT <: Sã®æã«Foo[S] <: Foo[T]ã¨ããæå³ãéå¤(nonvariant)ã¯ãããã®é¢ä¿ãæãç«ããªããã¨ã§ãããã©ã«ãã§ã¯ãããJavaã®é åã¯å ±å¤ã§ãããObjectã«ã¯ä»»æã®FooBarãå ¥ããããã¯Stringã«Objectãä»ãã¦Integerãå ¥ãããã¦ãã¾ãã¨ãããã¨ã§ãå®è¡æã¨ã©ã¼ãå¼ãèµ·ãããã´ã¹ãªã³æ°ãé åãã¸ã§ããªãã¯ã«æ±ãæ¹æ³ã欲ããã£ã(P.358)ãããä»ã§ã¯Javaã«ãã¸ã§ããªã¯ã¹ã®æ©è½ãå ¥ã£ã¦ããã®ã§é åã®å ±å¤ã¯å¿ è¦ã§ã¯ãªããäºææ§ã®ããã«æ®ããã¦ããã
ä¸éå¢çãdef append[U >: T](x: U)ãªã©ã¨ããæ¸ãæ¹ãã§ãããTãTã®ã¹ã¼ãã¯ã©ã¹ã§ãããã®ãå¼æ°ã«åãã¨ããæå³ãä¸éå¢çãããã[T <: Ordered[T]] lazy val x = ...;ã§é 延è©ä¾¡ æé»ã®åå¤åãx + yããªãã¨ãã«convert(x) + yãå¯è½ã§ã¯ãªãã調ã¹ããã«éãã®æ¹æ³ãããã¨ãã¯ã³ã³ãã¤ã«ã¨ã©ã¼ãåå¤æã¯1åããããªããæ¸ããã¦ããã¨ããã§åãå ´åã«ã¯å¤æããªãã ãããé§ç®ã ããããã«ä¸æ°ã«èªãã®ã¯è³ãç²ãããã¾ã å ±å¤ã»åå¤ã®è©±ã¨æé»ã®åå¤æãæ®ã£ã¦ããã ãã©ãç¶ãã¯ä»åº¦ã(追è¨ãã)