Scala 2.10.0から導入された文字列の保管(string interpolation)が早速便利でした。
Scalaに限らず色々な言語で文字列と変数を繋げて一つの文字列にするときは以下の様に書くと思います。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val strVal = "Yamada tarou" | |
val aisatu = "Hello, " + strVal | |
println(aisatu) | |
//Hello, Yamada taro |
string interpolationを使うと以下のように書けます。
先頭のsと$の部分がミソです。+とか”とか書かなくて済むのでいいですね。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val strVal = "Yamada tarou" | |
val aisatu = s"Hello, $strVal !" | |
println(aisatu) | |
// Hello, Yamada tarou ! |
変数がString以外でも以下のように使えます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val intVal = 2 | |
val nakamiha = s"中身は$intVal です." | |
println(nakamiha) | |
// 中身は2 です. |
但し、変数名の後にスペースが無いと以下のようにエラーが出ます。
どこまでが変数名なのかが分からないので当然と言えます。
そういう時は以下のように書けば問題ないです。
${}がミソ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val intVal = 2 | |
val daijoubu = s"中身は${intVal}です." | |
println(daijoubu) | |
// 中身は2です. |
英文の文字列を構築するときは単語間にスペースが入るので問題ないですが、日本語だとスペースが入らないことが普通なので、{}で囲む機会が増えそうです。
あとClassのメソッドとかも呼べます。
但し、これも{}を付けないと悲しいことになります。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Iremono[A](nakami: A) | |
val iremono = Iremono("中身") | |
println(s"中身 is $iremono.nakami") | |
// 中身 is Iremono(中身).nakami | |
println(s"中身 is ${iremono.nakami}") | |
//中身 is 中身 |
なのでちゃんと{}で囲みましょう。
string interpolationは{}の中味を評価した結果が文字列中に展開されるイメージなので、以下のように式も書ける。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
println(s"1 + 1 = ${ 1 + 1 }") |
凄い!!!
式が書けるので、代入も出来る。Scalaは代入の評価結果はUnitなので()になる。
()はUnitが持つ値で、要素数ゼロ個のタプルらしい。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
println(s"1 + 1 = ${ val hoge = 1}") | |
// 1 + 1 = () | |
println(s"1 + 1 = ${ val hoge = 1; hoge }") | |
// 1 + 1 = 1 | |
//println(hoge) // error |
当たり前だが外側からは参照できない。
他にも色々書き方があるので下のサイトが凄くわかりやすいです。
http://docs.scala-lang.org/ja/overviews/core/string-interpolation.html
そんなこんなでstring interpolationでした。
0 件のコメント:
コメントを投稿