滅入るんるん

何か書きます

KotlinはどれほどC#を意識しているか?

今週のC#記事はKotlinがどれほどC#を意識しているかにします。(Bloggerを開く前まではReactivePropertySlimがどれだけ軽くなったのか調べて書こうかなと思ってたのですが新鮮なネタのほうから書くことにします)

新鮮なネタと言ってますが、あくまで自分の中でです。
なぜ新鮮かというとこちらのKotlin公式サイト(https://kotlinlang.org/)と言うんでしょうか?オフィシャルなサイトの右上のサイト内検索で"C#"を検索するとちゃんとC#の単語出現場所を検索してくれることに気づいてしまったからです。

C#erならわかるかと思いますが、C#って検索システムによってはちゃんと検索してくれないんですよね…"C"で検索した扱いになったりとか。Google大先生はもちろんちゃんとC#で検索できるんですが、内部で特殊扱いしているのか、Q#やP#だと"Q"や"P"の検索結果が目立ちます。

はい、本題に戻りまして、kotlinlang.orgでのC#検索結果がこちらになります。


出現個所をまとめると

  • Extensions
  • Android Frameworks Using Annotation Processing
  • FAQ
  • Generics
  • Control Flow: if, when, for, while
  • Coroutines
  • Functions
ですね。
 

詳細

  • Extensions
Kotlin, similar to C# and Gosu, provides the ability to extend a class with new functionality without having to inherit from the class or use any type of design pattern such as Decorator. This is done via special declarations called extensions. Kotlin supports extension functions and extension properties.
ざっくり言うとC#のように関数(メソッド)を拡張する機能を提供しているとうこと。KotlinはC#と違い拡張プロパティなどにも対応していますが、言語デザインとしてはC#の拡張メソッドを参考にしているのでしょうか。拡張できる機能としてはSwiftのExtensionsに似ている気もしますがね。
(それにしてもGosuとはなんでしょうか。Go langのことですかね…?検索しても出てこなかった…)
  • Android Frameworks Using Annotation Processing
SQLiteライブラリであるDBFlowについて
That gives you a way to express queries via C#-like LINQ syntax, use lambdas to write much simpler code for asynchronous computations, and more. Read all the details here.
はい、お恥ずかしいながらDBFlow初めて知りました。内容としてましてはLINQ to SQLを移植したようなもの。hereのリンク先はこちら(https://agrosner.gitbooks.io/dbflow/content/KotlinSupport.html)

  • FAQ
Is Kotin hard?についての解答
Kotlin is inspired by existing languages such as Java, C#, JavaScript, Scala and Groovy. We've tried to ensure that Kotlin is easy to learn, so that people can easily jump on board, reading and writing Kotlin in a matter of days. Learning idiomatic Kotlin and using some more of its advanced features can take a little longer, but overall it is not a complicated language.
 言ってることは、Java・C#・JavaScript・Scala・Groovyなどの既存言語に影響を受けてるから少し勉強したらできると思うよ、ということ。

はい、ここではっきりとC#は意識の中に入っていることが明言されています。
JVM言語を除けばC#とJavaScriptのみであり、JavaScriptはトランスコンパイル対象であることからわざわざC#が明言されているということは明示的に意識したと解釈していいのでしょうかね。
  • Generics
Declaration-site varianceについて
We believe that the words in and out are self-explaining (as they were successfully used in C# for quite some time already), thus the mnemonic mentioned above is not really needed, and one can rephrase it for a higher purpose:
The Existential Transformation: Consumer in, Producer out! :-)
 共変性と反変性についてC#のアプローチがうまくいっていると書いてあるので、これについてはC#のアプローチのまま取り入れたということでしょう。
  • Control Flow: if, when, for, while
(これについては引用は割愛)
どこで取り上げられているかと言いますと、for(C#で言うところのforeachに該当する機能)での順番に列挙するにはiteratorが必要になるが、それらはC#と同じようにダックタイピングできるよ、ということ。
C#と違う点はoperator修飾子が必要ということでしょうか。
  • Coroutines
ここでは2箇所(3単語)で取り上げられています。

概要:
Many asynchronous mechanisms available in other languages can be implemented as libraries using Kotlin coroutines. This includes async/await from C# and ECMAScript, channels and select from Go, and generators/yield from C# and Python. See the description below for libraries providing such constructs.
The inner workings of coroutines:
More details on how coroutines work may be found in this design document. Similar descriptions of async/await in other languages (such as C# or ECMAScript 2016) are relevant here, although the language features they implement may not be as general as Kotlin coroutines.
1箇所目は、C#とECMAScriptのasync/awaitや、Go langのチャンネルとセレクトや、C#とPythonのgeneratorsとyieldはKotlinのコルーチンを使えば実装できるよ、ということ。 
2箇所目は、他の言語のasync/await(つまりC#とECMAScript)は重要だが、Kotlinのコルーチンと同じように一般的ではないかもしれない、ということ(おい)
 
コルーチンに関してはC#の影響を色濃く残していますが、async/awaitはそんなに一般的じゃないんですかね?まぁ少なくともJVM界隈では一般的ではないですね。最近はSwiftで実装するかもという話にもなっていますし、async/awaitはどんどん輸出されるものと思います。
  • Functions
Function Scopeについて
In Kotlin functions can be declared at top level in a file, meaning you do not need to create a class to hold a function, like languages such as Java, C# or Scala. In addition to top level functions, Kotlin functions can also be declared local, as member functions and extension functions.
 Kotlinではトップレベルで関数を宣言できるからJavaやC#やScalaのようにクラスである必要はないよ、ということ。
C#の影響は完全に受けていない文章ですが、なぜここでC#に触れるのか。まぁC#を意識しているんでしょうねぇ…

まとめ

公式的に見てもC#の影響を受けていたり、意識をしていてりはすると判断できると思います。
 
また、コミュニティ的にもPartial Class support in the future?でC#のpartial classみたいなのはないのか~?のようにC#の影響は結構あったりします(笑)
自分も昔LINQをKotlinに移植してみたりしましたが(負の遺産)
まぁ、それもそのはずC#を知っている人間がAndroid開発をJavaですることに耐えれるわけもなく…C#のような機能のあるKotlinに逃げるのは必然と言いますか自然の摂理と言いますか…
 
まぁAndroidではXamarin.Androidがあるので皆さんはKotlin使わずにC#を使ってどうぞ(ダイレクトマーケティング)