Kotlin编码规范

来源:互联网 发布:sql同字段求和语句 编辑:程序博客网 时间:2024/04/28 07:47

资料原文:http://www.kotlinlang.org/docs/reference/coding-conventions.html

本文包含了KotLin语言的当前编码格式。

命名格式

默认情况下采用Java编码规范:

  1. 采用驼峰式命名
  2. 类型以大写字母开头
  3. 方法和属性以小写字母开始
  4. 使用4个空格缩进
  5. 公有方法应该有类似Kotlin的文本文档

冒号

分开类型和父类的冒号之前有一个空格,在类型和实例分割的冒号钱没有空格

interface Foo<out T : Any> : Bar {    fun foo(a: Int): T}

Lambdas表达式

In lambda expressions, spaces should be used around the curly braces, as well as around the arrow which separates the parameters from the body. Whenever possible, a lambda should be passed outside of parenthes

list.filter { it > 10 }.map { element -> element * 2 }

lambdas which are short and not nested, it’s recommended to use the it convention instead of declaring the parameter explicitly. In nested lambdas with parameters, parameters should be always declared explicitly.

Unit

如果一个函数的返回类型为Unit,这个返回类型可以被忽略,该关键字的作用是函数不需要返回:

fun foo() { // ": Unit" is omitted here}
1 0
原创粉丝点击