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並みに記号が多い気がする。 特に _ アンダーバー使いすぎ。

This entry was posted on Thu, 12 Jun 2008 19:11:00 GMT and Posted in . You can follow any any response to this entry through the Atom feed. You can leave a comments, Or a trackback from your own site.
Tags ,


Trackbacks

Use the following link to trackback from your own site:
http://www.jaro68.jp/sett4/blog/trackbacks?article_id=scala%E3%81%A7%E5%8F%AF%E5%A4%89%E9%95%B7%E5%BC%95%E6%95%B0%E3%81%AE%E3%81%A8%E3%81%93%E3%82%8D%E3%81%AB%E9%85%8D%E5%88%97%E3%82%92%E6%B8%A1%E3%81%99&day=13&month=06&year=2008

Comments

Leave a response

Leave a comment