Weitere ähnliche Inhalte Ähnlich wie Scala2.8への移行 (20) Kürzlich hochgeladen (11) Scala2.8への移行2. • ScalaAPI
– 2.8
• http://www.scala-
lang.org/archives/downloads/distrib/files/nightly/do
cs/library/index.html
– 2.7
• http://www.scala-lang.org/docu/files/api/index.html
– sbazでローカルにダウンロード
• > sbaz install scala-devel-docs
– $SCALA_HOME/docs
• ソースリポジトリ
– http://www.scala-lang.org/node/213
– http://lampsvn.epfl.ch/trac/scala/browser/scala
3. • Java5以降に対応
• 余談:Java1.4を捨ててソースがすっきり。
– 例:String#format
• 2.7:
– http://lampsvn.epfl.ch/trac/scala/browser/scala/tags/R_2_
7_7_RC1/src/library/scala/runtime/RichString.scala
• 2.8:
– http://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/li
brary/scala/collection/immutable/StringLike.scala
4. へ移行する際の注意点
Scala 2.8へ移行する際の注意点
• ~Web Flavorの場合~
の場合~
– http://d.hatena.ne.jp/kmizushima/20100223/1
266940545
– を読んで勉強させていただきました。
• Web Flavor (@keisuke_nさん作)
– http://webflavor.sourceforge.net/index_ja.html
13. 配列と
配列とSeqの組み合わせに注意
の組み合わせに注意
2.7
scala> val some = if( 1 > 0 ) Array(1,2,3) else Seq(1,2,3)
some: Seq[Int] = Array(1, 2, 3)
ArrayはSeqのサブタイプ
2.8
scala> val some = if( 1 > 0 ) Array(1,2,3) else Seq(1,2,3)
java.lang.ClassCastException: [I cannot be cast to scala.ScalaObject
scala> val some = if( 1 < 0 ) Array(1,2,3) else Seq(1,2,3)
some: ScalaObject = List(1, 2, 3)
型と同等になった。
ArrayはJavaの T[ ] 型と同等
の
※でもAnyRefとかになってもよくね?
14. 配列と
配列とSeqの組み合わせに注意
の組み合わせに注意
2.8
scala> val some:Seq[Int] = if( 1 > 0 ) Array(1,2,3) else Seq(1,2,3)
some: Seq[Int] = Array(1, 2, 3)
scala> some.getClass
res13: class scala.collection.mutable.WrappedArray$ofInt
型を明示すれば implicit conversionが働く。
Predef.wrapArrayを参照
16. によるメソッドオーバーローディング
implicit conversionによるメソッドオーバーローディング
2.7 List.findはAPI上に既に存在するメソッドであることに注意!
scala> class RichListString(xs: List[String]) {
def find(x: String) = xs.find{_ == x} //(1) こっちはOK
def find(x: String, dummy: String) = xs.find{_ == x} //(2) 2.8ではこれがダメ
}
implicit def enrich(xs: List[String]): RichListString = new RichListString(xs)
List("A", "B", "C").find("A", "c")
res2: Option[String] = Some(A)
2.8
scala> List("A", "B", "C").find("A", "c")
error: too many arguments for method find:
List.findメソッドはAPIに既に存在しているために解決に失敗。
逆に(1)が動いてしまうのが微妙なところ。
>同名メソッドをimplicit定義するのは避ける
17. 誤解のないように・・・・
2.8 こういうのは全然OK
class RichListString(xs: List[String]) {
def firstElem() = xs.head
def firstElem(n:Int) = xs.head + n
def firstElem(n:Int,m:Int) = xs.head + n + m
}
20. へ移行する際の注意点
Scala 2.8へ移行する際の注意点
アプリの場合~
• ~Liftアプリの場合~
アプリ
– 弊社プロジェクトのLiftアプリケーションを
Scala2.8でコンパイル。
– エラーやWarningが出た箇所をご報告。
– 業務Webアプリなので、さほど込み入ったことは
していない。
21. Lift2.0をScala2.8で動かすには・・・・
mvn archetype:generate -U -DarchetypeGroupId=net.liftweb ¥
-DarchetypeArtifactId=lift-archetype-basic ¥
-DarchetypeVersion=2.0-scala280-SNAPSHOT ¥
-DarchetypeRepository=http://scala-tools.org/repo-snapshots ¥
-DremoteRepositories=http://scala-tools.org/repo-snapshots ¥
-DgroupId=yours -DartifactId=yourapp
Scalaバージョンには"2.8.0.Beta1"を入力
Simple Build Toolで動かすには
http://blog.takeda-soft.jp/blog/show/369
22. メモリを調整しておくと良いかも
pom.xml に追加
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<id>scala-compile</id>
</execution>
</executions>
<configuration>
<jvmArgs>
<jvmArg>-Xmx256m</jvmArg>
<jvmArg>-Xss1024K</jvmArg>
<jvmArg>-XX:PermSize=64m</jvmArg>
</jvmArgs>
</configuration>
</plugin>
23. SHtml (ガクブルな仕様変更)
class AForm {
object name extends RequestVar[String](S.param("name").openOr(""))
def add(xhtml: NodeSeq):NodeSeq = {
bind("f", xhtml,
"name" -> SHtml.text( name.is, name = _ ) ,
"submit" -> SHtml.submit("送信", doAdd) )
}
}
Re: Scala 2.8 Eclipse plugin beta - compilation errors in Lift
There is a change to type inferencing in 2.8.
The construct:
SHtml.text( hours, hours = _ )
No longer seems compiler-friendly. Try:
SHtml.text( hours, x => hours = x )
※ Exploring Liftのサンプルには多い記述方法
24. SHtml
class AForm {
object name extends RequestVar[String](S.param("name").openOr(""))
def add(xhtml: NodeSeq):NodeSeq = {
bind("f", xhtml,
"name" -> SHtml.text( name.is, name(_) ) , // (x)=>name(x)の糖衣
"submit" -> SHtml.submit("送信", doAdd) )
}
}
この書き方で統一していたので助かった ;x;
ただ、なんでこっちだと助かったのかはわからない。。
25. AnyVar / MappedField からのimplicit conversion
class AForm {
object price extends RequestVar[String](S.param("price").openOr("0"))
def doAdd() {
val charge = ShippingCharge.find(1).charge //DBからカラムを取得
Order.create.totalPrice( price.toInt + charge ).save
}
} // Scala2.7では、良きに計らってくれてた
class AForm {
object price extends RequestVar[String](S.param("price").openOr("0"))
def doAdd() {
val charge = ShippingCharge.find(1).charge //DBからカラムを取得
Order.create.totalPrice( price.is.toInt + charge.is ).save
}
} // Scala2.8では、コンパイルエラー。
// 「is」を明示。Convert the field to its "context free" type
26. Collection のAPI変更(deprecatedが多い)
Seq.first (のサブクラス全部)
-> IterableLike.head
//aliasが張られているだけ。
@deprecated("use `head' instead") def first: A = head
Map.keys / values
-> MapLike.keysIterator / valuesIterator
//alias
@deprecated("use `valuesIterator' instead")
def values: Iterator[B] = valuesIterator
27. Collection のAPI変更(deprecatedが多い)
List.sort( (A,A) => Boolean )
-> SeqLike.sortWith( (A,A) => Boolean )
List.sortはまだ独自ロジックが残っているので注意。
こんなコメントが残ってる!
!!! todo: move sorting to IterableLike
傾向:
○○Likeクラスにメソッドをまとめて、
各CollectionではそれらをMix-inする形に統合している。
たいていコンパイル時にdeprecated警告が出て、
代替メソッドを丁寧に表示してくれるので比較的安心。
deprecatedが出た場合は、実装を覗いてみるとなお安心。
29. Scala東北MLより
• Traversable#withFilter
– http://groups.google.co.jp/group/scala--
tohoku/browse_thread/thread/3a12002a643b
959f?hl=ja
• else の無い if 式の型
– http://groups.google.co.jp/group/scala--
tohoku/browse_thread/thread/c737c1940441d
2b0?hl=ja
30. 勉強会@東北
• Scala勉強会@東北
– Changes and Improvements on Scala 2.8
• http://sites.google.com/site/scalatohoku/changes-
and-improvements-on-scala-2-8
• 抜粋
• Redesigned collection libraries
• Named and default arguments
• Support for continuations
• ( )
Type specialization(@specialized annotation)
• @tailrec and @switch
• 山中さん素晴らしいっっ
31. 新機能-Scala東北ML
• scala.reflect.Manifest
– http://groups.google.co.jp/group/scala--
tohoku/browse_thread/thread/2858a1b7807929c5?hl
=ja
• scala.util.control.Breaks
– http://groups.google.co.jp/group/scala--
tohoku/browse_thread/thread/f4475f1d0da0181f?hl=j
a
• パターンマッチの最適化のチェック(@tailrec)
– http://groups.google.co.jp/group/scala--
tohoku/browse_thread/thread/ac3da34f805ee6c2?hl=
ja
32. 新機能-Scala東北ML
• 自己末尾再帰関数呼び出しの最適化
– http://groups.google.co.jp/group/scala--
tohoku/browse_thread/thread/6af4efd83a8e0
328?hl=ja
• case class - copy メソッド
– http://groups.google.co.jp/group/scala--
tohoku/browse_thread/thread/72211849f32aa
5e4?hl=ja
34. A Taste of 2.8
• Scala Collections
– http://www.scala-lang.org/node/2060
• Named and Default Arguments
– http://www.scala-lang.org/node/2075
• Continuations
– http://www.scala-lang.org/node/2096
• The Interactive Interpreter (REPL)
– http://www.scala-lang.org/node/2097
35. Scala2.8から
体系的に勉強したい人へ
• Oreilly:Programming Scala
– http://programming-scala.labs.oreilly.com/
37. • Changes in Version 2.8.0
– Trailing commas
• Trailing commas in expression, argument, type or pattern sequences are no longer
supported.
• Changes in Version 2.8 (under development)
– Changed visibility rules for nested packages (where done?)
– Changed visibility rules in §2 so that packages are no longer treated specially.
– Added section §3.5.3 on weak conformance. Relaxed type rules for
conditionals,match expressions, try expressions to compute their result type
using least upper bound wrt weak conformance. Relaxed type rule for local type
inference so that argument types need only weekly conformto inferred formal
parameter types. Added section on numeric widening in §6.25 to support weak
conformance.
– Tightened rules to avpod (avoidのtypo?) accidential overrides in §5.1.4.
– Removed class literals.
– Added section §7.4 on context bounds.
– Clarified differences between isInstanceOf and pattern matches (§12.1).
– Allowed implicit modifier on function literals with a single parameter (§6.23).
38. の確認
Change Logの確認
• Trailing commas
– List(1,2,3,) // 2.7 ではOKだけど、2.8ではコンパイルエラー
• Weak Conformance
– List( 1, 1.0 )
• 2.7では List[AnyVal] = List( 1, 1.0 )
• 2.8では List[Double] = List( 1.0, 1.0 ) //
39. の確認
Change Logの確認
(???)
• Context Bounds(???)
– 2.7
• class Animal
• class Dog[T]
• implicit object dog extends Dog[Animal]
• class Monkey[T]( implicit dog:Dog[T] )
• new Monkey[Animal]
– 2.8
• class Animal
• class Dog[T]
• implicit object dog extends Dog[Animal]
• class Monkey[T:Dog]
• new Monkey
40. にはないが追記されてること
Change Logにはないが追記されてること
• 3.7 Type Erasure
– compound typeのerasureについて
• 4.3 Type Declarations and Type Aliases
– Example 4.3.2 Pair をTuple2のAliasとして定義してる例
の として定義してる例
• 4.6 Function Declarations and Definition の最後の節
• 5.1.1 Constructor Invocations
– 6.6.1 Named Parameter に関連して追記
• 5.3.2 Case Classes
メソッド追記
– copyメソッド追記
• 6.6 Function Applications
– call-by-name/call-by-value のくだり変更
• 6.6.1 Named and Default Arguments 追記
• 6.20 Return Expressions
内部無名関数からのreturnの挙動
– 内部無名関数からの の挙動
41. にはないが追記されてること
Change Logにはないが追記されてること
• 6.23 Anonymouse Functions
無名関数でNamed Parameterをimplicitにしたときの挙動
– 無名関数で を にしたときの挙動
• 6.25 Implicit Conversions
– Numeric Widening (Weak Conformするときの変換について)
• 6.25.3 Overloading Resolution
– よくわからないけど、記述が大幅に変わってます><
• 7.5 Manifests
– To be written..
• 8.1.3 Pattern Binders
• 8.1.8 Extractor Patterns
– Example 8.1.3 追記
• Chapter11 User-Defined Annotations
の 定義が削除
– NameValuePairのSyntax定義が削除
42. ブログなど
• Bay-Area Scala Enthusiasts (BASE)
Meeting: What's New In Scala 2.8
– http://blog.objectmentor.com/articles/2009/06/
05/bay-area-scala-enthusiasts-base-meeting-
whats-new-in-scala-2-8
• Control flows in Heaven with Scala 2.8
continuations
– http://ahardcodersday.blogspot.com/2009/08/
control-flows-in-heaven-with-scala-28.html
– http://ahardcodersday.blogspot.com/2009/08/
control-flows-in-heaven-with-scala-28_27.html