Takuya71 のぶろぐ

外資系ソフトウェア会社で働いてます、認定スクラムマスター

Scalaz7.0.0-M3では ApplicativeBuilder の |@| が deprecation となった

最近 scalaz がお気に入りでいろいろ試しているのですが、
昨日 scalaz-7.0.0-M3 に上げたところ、
|@| を使うと deprecation の 警告がでるようになりました。

scala> 1.some |@| 3.some
warning: there were 1 deprecation warnings; re-run with -deprecation for details

scala を -deprecation オプション付けて起動するために、

build.sbt の設定を変更します。

scalaVersion := "2.10.0-M7"

resolvers += "Typesafe Snapshots" at "http://repo.typesafe.com/typesafe/snapshot
s/"

libraryDependencies ++= Seq(
  "org.scalaz" % "scalaz-core_2.10.0-M7" % "7.0.0-M3"
)

scalacOptions ++= Seq("-feature","-deprecation")


initialCommands in console := "import scalaz._, Scalaz._" 

sbt console でコンソールを起動します。

> sbt console

同じことをしてみます。

 scala> 1.some |@| 2.some
 <console>:14: warning: method |@| in trait ApplyOps is deprecated: Use `^(f1,f2.
.fN)((a,b,c) => ..)` instead
              1.some |@| 2.some

|@| の代わりに ^(f1,f2..fN)((a,b,c) => ..) をつかえと言われます。

じゃ ってことで使ってみます。

scala> ^(1.some,2.some,3.some)(_ + _ +_)
res1: Option[Int] = Some(6)

警告もなく |@| と同じ結果となりました。
見た目は |@| の方が好きなのですが。。。

Github のソース見るとここで CleanUp が入っているようです。


この記法つかうと 普通に scala で for 使って書くと

scala> for (
| i <- List(1,2,3);
| j <- List(4,5,6)
| ) yield i * 1 + j * 1
res46: List[Int] = List(5, 6, 7, 6, 7, 8, 7, 8, 9)

こんなのが

scala> ^(List(1,2,3),List(4,5,6))(_*1 |+| _*1)
res47: List[Int] = List(5, 6, 7, 6, 7, 8, 7, 8, 9)

というように記述できます。

次に case class に apply する場合

scala> case class User(name:String,id:String,age:Int)
defined class User

scala> ^("takuya".some,"12345".some,5.some)(User(_,_,_))
res48: Option[User] = Some(User(takuya,12345,5))

scala> res48.get.name
res49: String = takuya

scala> res48.get.id
res50: String = 12345

scala> res48.get.age
res51: Int = 5

こんな感じで

2012/09/11 追記 ***

|@| が deprecated から外れたようです。
次のM4からは復活かな