coding with kotlin

作者: f745cc92b2d5 | 来源:发表于2018-08-04 14:24 被阅读18次

kotlin 简介

JetBranis 于 2016.2.15 发布 kotlin v1.0 版本, 现在官网的最新版本是 v1.2.60。 官网解释为:

Statically typed programming language
for modern multiplatform applications

100% interoperable with Java™ and Android™

和 java 一样,都是跨平台的静态语言。并且百分百兼容 java 和 android 平台。据说 kotlin 曾连续几个月在 TIOBE 上排 49,由此可见其稳定性(>.<)。


tiobe-2018-08

关于 kotlin 的具体介绍,网上已经很多了, 在此不再赘述。

kotlin 基本语法

  • kotlin doc 官网上已经描述的很详细了。 如果有一定的 java 基础, 可以很轻松上手。
  • kotlin with spring boot
    1. create your maven project with spring boot .

    2. add kotlin dependencies( version: 1.2.51) to your pom.xml

       <dependency>
          <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
        </dependency>
      
    3. add kotlin plugin

          <plugin>
               <artifactId>kotlin-maven-plugin</artifactId>
               <groupId>org.jetbrains.kotlin</groupId>
               <configuration>
                   <args>
                       <arg>-Xjsr305=strict</arg>
                   </args>
                   <compilerPlugins>
                       <plugin>spring</plugin>
                   </compilerPlugins>
               </configuration>
               <dependencies>
                   <dependency>
                       <groupId>org.jetbrains.kotlin</groupId>
                       <artifactId>kotlin-maven-allopen</artifactId>
                       <version>${kotlin.version}</version>
                   </dependency>
               </dependencies>
           </plugin>
      
    4. create Main method

      @SpringBootApplication
      class OwWebApplication
      
      fun main(args: Array<String>) {
          SpringApplication.run(OwWebApplication::class.java, *args)
      }
      
    5. create your own controller with annotations just like using java with spring

        @RestController
        @RequestMapping("/api")
        @Api("health check")
        class HealthCheck {
            @GetMapping("/ping")
            fun healthCheck(): String {
                return "PONG"
            }
          }
      
    6. now you can start your service

    7. 具体参见 ordinary-world
      温馨提示: 注意切换分支。目前主要在 develop 分支

个人体验

  1. 对 NPE 友好。
    kotlin type check: kotlin 的 基类为 Any(java 中 的 Object)。 kotlin 的类型检查器在编译期间对所有基于 Any 的子类作检查时, 如果有 NPE, 则会报错。
    那么, 如果需要接收空类型时该如何呢?别急, kotlin 里还有这个: Any? 。我看到的一些资料里认为 Any? 是 Any 的父类, 也就是说 Any? 才是 kotlin 里的爸爸。 但我觉得这种类型是 Any 的一个映射, 通过 kotlin 的 type checker 做 NPE check。 查看 kotlin 的源码也可以看到, 只有 Any 类, 并未有什么 Any? 。同时, 该类中的注释也说的很清楚:

    The root of the Kotlin class hierarchy. Every Kotlin class has [Any] as a superclass.

  2. 强大的语法糖。
    相对于 java 在 8 之后支持的 lambda(已经很好用了),用完 kotlin 后,表示十分酸爽。当然, 由于见识的问题, 并没有怎么用过专业的函数式编程语言(Lisp,erlang, scala,haskell 之流),所以 kotlin 的函数式编程对我而言已经很好了。

  3. 支持类扩展

      fun main(args: Array<String>) {
            println(">,<".extension())
      }
      fun String.extension(): String {
          return "$this-class-extension"
      }
    

    用过都说好

  4. 数据类型
    kotlin 提供 data class 作数据类, 告别 getter/setter/equals/hashCode 等。当然, java 可以通过 Lombok 来搞定这些。

  5. 其他静等各位使用后总结了。


  6. 实际使用中遇到过一次。 参见 kotlin using jackson annotation

Reference

  1. wiki kotlin
  2. tiobe
  3. Kotlin
  4. Kotlin TYPE CHECK
  5. Kotlin TYPE CHECK 译文

相关文章

网友评论

    本文标题:coding with kotlin

    本文链接:https://www.haomeiwen.com/subject/dsipvftx.html