2013年7月22日月曜日

Kotlin勉強会に行くのでちょっとだけ予習した

多分日本初という触れ込みの勉強会に行くので予習したメモ

ただし、主催者は現状こんな感じらしい





kotlinの未来が不安ですね…



というわけで超適当に以下のkotlinの基本文法を眺めてみた

https://sites.google.com/site/tarokotlin/3-kotlinno-biao-zhunapi



そうそう、 Comparision to Scala ってページがあるので開くと…

If you are happy with Scala, you probably don't need Kotlin.

らしいです

あれ、じゃあ終わり?



スマートキャスト


is演算子で型チェックしてたら、それはもうその型とみなして動かしてええで!
みたいな感じらしいです

fun sayHello(target : String) {
println("Hello, $target!")
}
val target : Any = "world"
if(target is String) {
sayHello(target) // Hello, world!
}
view raw 1.kt hosted with ❤ by GitHub


ちなみにScalaにはねーよってDocumentに書いてます

同じようなことしようとするとこんな感じ?

def sayHello(target : String) = println(s"Hello, $target!")
val target : Any = "world"
target match {
case x:String => sayHello(x)
case _ =>
}
view raw 1.scala hosted with ❤ by GitHub


うん、パッとは思いつかんけど同じような挙動だよね

関数


defかfunかの違いくらい?
ローカル関数もあるし〜

と思ったらローカル関数でなかなか面白いことが

fun isUpperCase(str : String) : Boolean {
fun String.all(f : (Char) -> Boolean) : Boolean {
for(val c in this) {
if(!f(c)) {
return false
}
}
return true
}
return str.all {
Character.isUpperCase(it)
}
}
view raw 2.kt hosted with ❤ by GitHub


拡張関数というものが混じっている

Scalaだったら暗黙の型変換とかで同様のことを頑張る感じ?

def isUpperCase(str : String) : Boolean = {
implicit class MyRichString(val self : String) {
def all(f : Char => Boolean) : Boolean = self.forall(f)
}
str.all(Character.isUpperCase)
}
view raw 2.scala hosted with ❤ by GitHub


まあサンプルと同じ内容なら

str.forall(_.isUpper)

でいいじゃん?とは突っ込まない

関数リテラル


kotlinだと関数は引数として渡せないよと。
渡すなら関数リテラルでと。

fun run(f : () -> Unit) = f()
// リテラルじゃないから引数として渡せないらしい
//fun sayHello() {
// println("Hello")
//}
val sayHello = {
println("Hello")
}
run(sayHello)
view raw 3.kt hosted with ❤ by GitHub


def run(f: () => Unit ) = f()
val runLiteral = (f:() => Unit) => f()
def sayHelloFunc() = println("Func!!")
val sayHelloLiteral = () => println("Literal!!")
run( sayHelloFunc )
run( sayHelloLiteral )
runLiteral( sayHelloFunc )
runLiteral( sayHelloLiteral )
view raw 3.scala hosted with ❤ by GitHub


scalaの場合、関数から関数リテラルに変換できるからだろうかどっちでもOK
そういや深く考えたことねーわ…

// aliasと言って良いのか…?
val alias = sayHelloFunc _

kotlinにも関数から関数リテラルに変換するやつがあれば結局一緒な気がする

when式


matchとどう違うのか… と思ったけど、割りとswitch文よりな感じ?
(ただしcontinue書いたらコンパイル通らなかったし、まだ未実装?)

なので5.2のパターンマッチングを見てみた

when(pair) {
is #(0, 0) -> "(0, 0)"
is #(0, Int) -> "(0, Int)"
is #(Int, 0) -> "(Int, 0)"
is #(Int, Int) -> "(Int, Int)"
is #(String, String) -> "(String, String)"
else -> "unknown"
}
view raw 4.kt hosted with ❤ by GitHub


val pair:(Any, Any) = ...
pair match {
case (0, 0) => "(0, 0)"
case (0, _:Int) => "(0, Int)"
case (_:Int, 0) => "(Int, 0)"
case (_:Int, _:Int) => "(Int, Int)"
case (_:String, _:String) => "(String, String)"
case _ => "unknown"
}
view raw 4.scala hosted with ❤ by GitHub


同じように書いたらこんな感じ?


ちなみにweb demoだと Tuples are not supported. Use data classes instead. って怒られる…
→ 後のデータクラスの章で悲しみの答えが!

流れでバインディングパターンも

val pair : #(String, Int) = #("taro", 23) // ← 先日25歳になっていたような…w
if(pair is #(val a, val b)) {
println("$a ($b)") // taro (23)
}
view raw 5.kt hosted with ❤ by GitHub


val pair = ("taro", 23)
// スマートキャストとおんなじような事やるならパターンマッチも使えばいいかな
val (a, b) = pair
view raw 5.scala hosted with ❤ by GitHub



分解者パターン:// あとで書く!


とあるけど、きっとExtractorとおんなじような感じなのかな…?と脳内で勝手に完結

for とか while とか ジャンプとリターン とか


ラベル付きのbreakができるよ!らしい
この辺とかはjavaよりも強力ですよ〜な売りになるんだろうか

その他


四章チラ見して記憶したこと

out/inで共変/反変

staticなメソッド持てないよ 代わりにobjectってあるよ

5章ちら(ry

https://sites.google.com/site/tarokotlin/chap5/sec54
null安全かOptionかは使う人によって良し悪し変わるんだろうなぁ…

https://sites.google.com/site/tarokotlin/chap5/sec55
データクラスってケースクラスみたいなもんかな?
そしてさり気に一番下に「マイルストーン3以降では、タプル機能は廃止されています。」
の文言が…/(^o^)\



各所チラ見した感想としては、確かにScalaで満足してたらKotlinは要らんよ?

って感じなんだろうなぁ…と

まあそんなこと言い出したら、別にどの言語でもそれでいいじゃんになりそうな気はする。

ScalaとKotlinって、それ(Scala | Kotlin)で見たよ!
みたいな感じにお互いがなるんですかね。

ってことでざっくりテキトー予習終わり

おまけ


sbt → simple build tool
kbt → kantan build tool

だったら面白かったのに…(これが書きたかっただけだったりw)

0 件のコメント:

コメントを投稿