Seasar Conference 2009 Autumnã§scalaã«ã¤ãã¦çºè¡¨ãã¾ããã
Seasar Conference 2009 Autumnã«ãJavaプログラマに捧げるScala入門ã¨ããã¿ã¤ãã«ã§ç»å£ããã¦é ãã¾ããã
å½æ¥ã¯ããã«ãã®å¤©åã«ãé¢ããããããããã®æ¹ã«ãæ¥å ´é ããããã¨ããããã¾ããã
çºè¡¨è³æã¯ãSeasar Conference 2009 Autumnã®ã»ãã·ã§ã³æ å ±ã®ãã¼ã¸ã«æ²è¼ããã¦ãã¾ãããSlideShareã«ãç½®ãã¦ããã¾ãã
ã¾ãããã¢ã«ã¤ãã¦ã¯å¥ã®ã¨ã³ããªã§è£è¶³ãã¾ãã
Seasar Conference 2009 Autumnでのデモ - ゆろよろ日記
Scalaã®è¨èªä»æ§ãä¸æ°ã«èª¬æããã®ã¯ç¡çã ã¨æã£ã¦ããã®ã§ãç´åã§æ§æãå¤ãã¦ãã¢ãæåã«è¦ããããã«ãã¾ããã
ã¾ãããã¢ãè¦ã¦ããã£ã¦ãScalaã£ã¦å¤æ
è¨èªã ããããã«ä½¿ãã®ã¯ç¡çããã?ãã£ã¦ããå
å
¥è¦³ãç ´å£ããä½æ¦ã§ãã
ãæ¥å ´ãããæ¹ã
ãã©ãæãããã¯ãããã¾ãããããã¡ãã£ã¨ä½¿ã£ã¦ã¿ãããã¨å°ãã§ãæã£ã¦ããã ãããããããã§ãã
å®éããã¢ã§è¦ãããS2Conteinerãscalaã®å¯¾è©±åã¤ã³ã¿ããªã¿ããæä½ãããæ¹æ³ã¯ãç§ãæºãã£ã¦ããæ¡ä»¶ã§æ´»ç¨ãã¤ã¤ããã¾ãã
å¾åãã£é£ã°ããé¨åã«ã¤ãã¦ã®è£è¶³
ãã£ã±ãæéä¸è¶³ã§ãScalaã«ããããé²åãããªãã¸ã§ã¯ãæåãã®é¨åã説æããããªãã£ãã®ãå¿æ®ãã§ãã
ããã§å°ãè£è¶³ãããã¨æãã¾ãã
ã£ã¦ããã¼ãScalaã®è¨èªä»æ§ã50åã§èª¬æã¨ãç¡è¬ãªãã¹ãã
å¾åçç¥ããtraitã¨ãcaseã¯ã©ã¹ãªããã¾ã åºã®å£ã§ãimplicitç³»ãGenericsãextractorãããããªã¢ã·ãã¤ãã ãã©ããã¾ã§ãã©ãçãã«ã¯2,3æéå¿
è¦ã ã£ã¤ã¼ã®ã
scala-beã§ãScalaã«ã¤ãã¦0ããã¿ã£ã¡ã説æãã¾ãã£!ãçãªåå¼·ä¼ã¨ãä¼ç»ãã¦ã¿ãããããã・・・ã
ã¯ã©ã¹ã¨ãªãã¸ã§ã¯ã
- ã¯ã©ã¹ã®å®ç¾©ã¯ãclass ã¯ã©ã¹å( å¼æ°å:å,...) extends ã¹ã¼ãã¼ã¯ã©ã¹ with Trait...{ ...}
- ã¯ã©ã¹ã®å®ç¾©ã§ã¯ãã³ã³ã¹ãã©ã¯ã¿å¼æ°ãæå®ã§ããã
- ã³ã³ã¹ãã©ã¯ã¿å¼æ°ã«val/varãä»ä¸ãããã¨ã§ãèªåã§ã¢ã¯ã»ãµãå®ç¾©
- ã³ã³ã¹ãã©ã¯ã¿å¼æ°ã«valãã¤ããã¨ãèªã¿åãå°ç¨ãã£ã¼ã«ãã¨ãªããclass Foo(val name) ã§nameãã£ã¼ã«ããèªã¿åãå¯è½ã
- ã³ã³ã¹ãã©ã¯ã¿å¼æ°ã«varãã¤ããã¨ãèªã¿åã/æ¸ãè¾¼ã¿å¯è½ãã£ã¼ã«ãã¨ãªããclass Foo(var name) ã§nameãã£ã¼ã«ããå¤æ´å¯è½ã
- ã³ã³ã¹ãã©ã¯ã¿å¼æ°ã«ãprivateãªã©ã®ã¢ã¯ã»ã¹ä¿®é£¾åãoverrideãæå®ã§ãã
- ã³ã³ã¹ãã©ã¯ã¿ãè¤æ°æãããå ´åã¯ãdef this( i:Int ... )ãªã©ã®ããã«def thisã§å®ç¾©ãã
- ã¡ã½ãããoverrideããå ´åã¯ãæ示çã«overrideãã¼ã¯ã¼ããä»ä¸ããªãã¨ã³ã³ãã¤ã«ã¨ã©ã¼ã¨ãªããovreride def toString( s:String) = "foo"
- ã¯ã©ã¹å®ç¾©ã¯ãã¹ãã§ãããã¤ã³ãã¼ã¯ã©ã¹ãã¤ã³ãã¼ãªãã¸ã§ã¯ããªã©ã
- object ãªãã¸ã§ã¯ãåã§ã·ã³ã°ã«ãã³ãªãã¸ã§ã¯ããå®ç¾©ã§ãã
- åä¸ã¹ã³ã¼ãã«åãååã®ã¯ã©ã¹ã¨ãªãã¸ã§ã¯ããåå¨ããå ´åãç¸äºã«privateã¢ã¯ã»ã¹ãå¯è½ãªç¹æ¨©çã¢ã¯ã»ã¹æ¨©ãä»ä¸ãããã(ã³ã³ãããªã³ãªãã¸ã§ã¯ã)
- staicãç¡ãScalaã§ã¯ãç¹å®ã¯ã©ã¹ã«é¢ããã¡ã¿æä½ã¯ã³ã³ãããªã³ãªãã¸ã§ã¯ãã«å®è£ ãããã¨ãå¤ãã
- ã³ã³ãããªã³ãªãã¸ã§ã¯ãã«applyã¡ã½ãããå®è£
ãã¦ãã¯ã©ã¹ã«å¯¾ãããã¡ã¯ããªã¡ã½ãããæä¾ãããã¿ã¼ã³ãå¤ç¨ãããã
- List(1,2,3)ã§Listãªãã¸ã§ã¯ããçæã§ããã®ã¯ãListãªãã¸ã§ã¯ãã®def apply[A](xs : A*) : List[A]ã¡ã½ããã§Listãªãã¸ã§ã¯ããçæãã¦ããã
class Foo ( s:String, i:Int ) { val p1:String = s var p2:Int = i private var p3 = 1 def hoge = s * p3 } object Foo { val aaa ="aaa" def setP3( foo:Foo, i:Int) = foo.p3 = i }
ä¸è¨ã®ãããªFooã¯ã©ã¹ã¨Fooãªãã¸ã§ã¯ãã¯ãç¸äºã«privateã¢ã¯ã»ã¹ã§ãããããFoo.setP3( foo,10)ã§fooãªãã¸ã§ã¯ãã®privateãã£ã¼ã«ãã®p3ãã£ã¼ã«ããå¤æ´ãã¦ããã
traitã¨mixin
- trait ã¯å®è£
ããã¦ãinterface
- ãããä¹±æ´ãªèª¬æãtraitã¯ãã³ã³ãã¤ã«ãããã¨ã¯ã©ã¹ã«ãªã
- ã©ã¡ããã¨ããã¨abstractã¯ã©ã¹
- classãobjectã«è¤æ°ã®traitãMix-In
- class Foo extends Bar with Baz
- Fooã¯ã©ã¹ã¯ã"Bar"traitã¨"Baz"traitãããã£ã¼ã«ããã¡ã½ãããç¶æ¿
- è¤æ°ã®traitãMixinããå ´åã¯ãã¯ã©ã¹é層ã¯"ç´åå"ãããã®ã§ããã¤ã¤ã¢ã³ãç¶æ¿åé¡ã¯çºçããªã
- class Foo extends Bar with Bazã¯ãFoo â Baz â Bar ã®ãããªç¶æ¿é¢ä¿ã«ãªã
- class Foo extends Bar with Baz
- trait ã®ä¸ã§å®è£
ãæããªãã¡ã³ãã¯abstract
- é¢æ°ä»¥å¤ã«ããã£ã¼ã«ããabstratæ±ã
- valã§å®£è¨ãããã£ã¼ã«ããdefã§é¢æ°ã¨ãã¦overrideã§ããã(éãå¯è½)
trait Bar { val bs:String = "Bar" def bar( n:Int ) = bs * n } trait Baz { def baz( n:Int ) = "Baz" * n } class Foo extends Bar with Baz
ä¸è¨ã®ããã«ã"Bar"traitã¨"Baz"traitãMixinããFooã¯ã©ã¹ã¯ã"Bar"traitã®baré¢æ°ã¨ã"Baz"traitã®bazé¢æ°ãç¶æ¿ãã¦ããã
scala> val foo = new Foo foo: Foo = Foo@4069b25f scala> foo.bar(1) res0: String = Bar scala> foo.baz(3) res1: String = BazBazBaz
caseã¯ã©ã¹ã¨ãã¿ã¼ã³ããã
- case class <ã¯ã©ã¹å>(ã³ã³ã¹ãã©ã¯ã¿å¼æ°)
- case class ã¯åå(...)ã§ã¤ã³ã¹ã¿ã³ã¹çæ( immutable object)
- case class Foo( i:Int,s:String)ã§ãFoo(1,"a")ã®ããã«ã¤ã³ã¹ã¿ã³ã¹çæ
- ã³ã³ãã¤ã©ããã¡ã¯ããªã¡ã½ãããçæãã
- case classã®ã³ã³ã¹ãã©ã¯ã¿å¼æ°ã¯ãã¹ã¦"val"ãä»ä¸ããã
- ã³ã³ã¹ãã©ã¯ã¿å¼æ°ã«å¯¾ãã¦èªåã§èªã¿åãå°ç¨ã®ã¢ã¯ã»ãµãå®ç¾©
- case classã§ã¯ãã³ã³ã¹ãã©ã¯ã¿å¼æ°ãå
ã«èªåçã«toString,equals,hashCodeã追å ããã
- Foo(1,"a") == Foo(1,"a") ã¯true
- case class ã¯åå(...)ã§ã¤ã³ã¹ã¿ã³ã¹çæ( immutable object)
- case ã¯ã©ã¹ã¯ãã¿ã¼ã³ã¨ãã¦ä½¿ãã
- matchå¼ã¯switchã®å¼·åãªãã¤
- å¤ match { case ãã¿ã¼ã³ => å¦ç ...}
- javaã®switchã¯å®æ°ãã許容ããªãããmatchå¼ã¯å¤/å/caseã¯ã©ã¹ãªã©ãæå®ã§ãã
- å®æ°ãã¿ã¼ã³ "aaa" => {...} â å®æ°ã¨ãã¦"aaa"ã«ããã
- å¤æ°ãã¿ã¼ã³ a => {println( a )} â aã«ã¯ãããããå¤ãæç¸ããã=> {...}ã®ä¸ã§ä½¿ç¨ã§ãããæ£è¦è¡¨ç¾ã®ãã¿ã¼ã³ãããã®ã¤ã¡ã¼ã¸
- ã³ã³ã¹ãã©ã¯ã¿ãã¿ã¼ã³ Foo(1,s) => {...} â Fooã¯caseã¯ã©ã¹ãªã©ãsã¯å¤æ°ãã¿ã¼ã³ã§ãä»»æã®å¤ã«ãããã
- åä»ããã¿ã¼ã³ case i:Int => {...} â Intåã«ããã
- ã¬ã¼ãæ¡ä»¶ case i if i > 0 => { ...} â i > 0ãæºããå ´åã®ã¿ããããã
- case _ => ã¯ããã©ã«ãã®ãã¿ã¼ã³
- ãã¿ã¼ã³ã¯ãè¨è¿°ããé çªã«è©ä¾¡ããã
- matchå¼ã¯å¿ ãå¤ãè¿ã
- ããããããã¿ã¼ã³ãç¡ãå ´åã¯ãMatchErrorãthrowããã
- ãããå¼ã®ç¶²ç¾
æ§ã¯ãã³ã³ãã¤ã©ã«ãã£ã¦ãã§ãã¯ããã
- å¿ ããããããªããã¿ã¼ã³ã¯è¦åãçºçãã
- ã·ã¼ã«ãã¯ã©ã¹
- ã·ã¼ã«ãcaseã¯ã©ã¹ã¨ãã¦ãããå¼ã«é©ç¨ãããå¯è½æ§ãããè¤æ°ã®ã¯ã©ã¹ãåå¨ããå ´åã¯ããã¹ã¦ã®ãã¿ã¼ã³ãè¨è¿°ããªãã¨ã³ã³ãã¤ã«ã¨ã©ã¼
- sealed abstract class Aãç¶æ¿ããcase classã«B,C,Dãããå ´å
- def test( a:A) = { a match { case B => {print("B")}, case C=>{print("C") }
- ããã¯ãDã®ãã¿ã¼ã³ãç¡ãããã³ã³ãã¤ã«ã¨ã©ã¼
sealed abstract class Pt case class CC(s:String) extends Pt case class DD(i:Int ,s:String) extends Pt def test(p:Pt) = p match { case CC( s ) => println( s ) case DD( 3 ,_) => println("match DD") }
ä¸è¨ã®ããã«æ½è±¡ã¯ã©ã¹Ptãç¶æ¿ãã2ã¤ã®case class CCã¨DDãããå ´å
scala> val cc = CC( "CCCC") // ãã¡ã¯ããªã§ã¤ã³ã¹ã¿ã³ã¹çæ cc: CC = CC(CCCC) scala> cc == CC("CCCC") // equalsããªã¼ãã©ã¤ãããã¦ããã®ã§å¥ã¤ã³ã¹ã¿ã³ã¹ã§ãtrue res2: Boolean = true scala> val dd1 = DD( 1,"DD1") dd1: DD = DD(1,DD1) scala> val dd2 = DD( 3,"DD2") dd2: DD = DD(3,DD2) scala> test( cc) // 1çªç®ã®ãã¿ã¼ã³ã«ããããã¦ãã CCCC scala> test ( dd1 ) // ãããããªãã®ã§ã¨ã©ã¼ scala.MatchError: DD(1,DD1) at .test(<console>:17) at .<init>(<console>:20) at .<clinit>(<console>) at RequestResult$.<init>(<console>:3) at RequestResult$.<clinit>(<console>) at RequestResult$result(<console>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.Delegati... scala> test (dd2) // 2çªç®ã®ãã¿ã¼ã³ã«ããããã¦ãã match DD
æå¾ã«
ãæ¥å ´ããã ããæ¹ã
ã«ã¯åã御礼ç³ãä¸ãã¾ãã
ã¾ããçºè¡¨ã®æ©ä¼ãä¸ãã¦ããã ããSeasarãã¡ã¦ã³ãã¼ã·ã§ã³ã®åä½ã«ãæ·±ãæè¬ãããã¨æãã¾ãã
ãããæ©ä¼ã«ãscalaã«èå³ããã£ã¦ããã人ãå¢ãã¦ãããã¨ãããªãã¨ãæãã¾ãã