Scalaで可変長引数のところに配列を渡す
pythonで書くころのコレを:
In [6]: def hoge(*a):
print a
...:
...:
In [8]: hoge(*(1,2,3,4,5))
(1, 2, 3, 4, 5)
Scalaで書くとこうなるみたい:
scala> def hoge(a:int*):Unit = { println(a) }
hoge: (int*)Unit
scala> hoge(1::2::3::4::5::Nil: _*)
List(1, 2, 3, 4, 5)
正直言って、 :_って何ですか?状態。そこの部分、:intとか書いちゃあかんのかなーとか思って試したらエラー。どうやら _* でsequence argumentですよーって印らしい。
The Scala Language Specification Version 2.7:
4.6.2 Repeated Parameters
The only exception to this rule is if the last argument is marked to be a sequence argument via a _* type annotation.
なーんか、Scalaってこういう記号が多いよなー。すっきりした文法とか言われてるけど、Perl並みに記号が多い気がする。 特に _ アンダーバー使いすぎ。
Scalaやり始めてみた
なんとなくScalaをやり始めてみた。
参考にしたのは以下のサイト。
- 気になる開発プロダクツ 第7回 Scala 2.6.0-final http://gihyo.jp/dev/serial/01/awdp/0007
- Using eclipse hotdeploy http://liftweb.net/index.php/Usingeclipsehotdeploy
Scala Plugin for Eclipseをインストールした後に、Mavenでliftのプロジェクトを作る。ScalaとLiftってmvnのプラグインがあるのがいいなー。
こんな感じでプロジェクトを作れる。mvn風に言うとアーティファクトが作れる。
mvn archetype:generate -U -DarchetypeGroupId=net.liftweb -DarchetypeArtifactId=lift-archetype-blank -DarchetypeVersion=0.8 -DremoteRepositories=http://scala-tools.org/repo-releases -DgroupId=jaro68.jp -DartifactId=helloLift
mvnがインストールされてれば、Using eclipse hotdeployのページのとおりにやってmvnで作ったプロジェクトをEclipseに投入できる。さらに書いてあるとおりに進めて、EclipseでHot Deployできた。楽チンやのー。
とりあえず、Hello Lift world。