如果我们静态编译Groovy,Kotlin和Groovy看起来是非常相似的语言,具有非常相似的特性.除了空安全性,Kotlin还有哪些功能是Groovy中缺少的?

推荐答案

Kotlin is a JVM language, which IMO is trying to improve on Java in features and conciseness, while remaining imperative and static. Groovy has a similar concept except decided to go dynamic. As a result, a number of language features will be similar.

以下是我意识到的一些差异

  • 静电VS动态:因为GroovyGroovy语言是作为一种动态语言设计的,而后来又添加了@CompileStatic,同时还添加了一个很棒的注释(我经常使用它).它的功能让人感觉有点死板,而且不会强迫人们以静电的方式编写代码.并不是所有地方都可以使用它(例如,我的Spock测试似乎无法与它们一起编译).有时,即使在Groovy上安装了它,似乎仍然时不时地会有一些奇怪的动态行为.Kotlin 是百分之百的静电,动态不是一个选项.

There are a number of other features that is has though. I'd recommend you look at the reference, and you may spot a few more e.g. https://kotlinlang.org/docs/reference/

  • 数据类——简洁,带有复制函数(有点像Scala中的case类)
  • The null safety check you mentioned (which is a big pro)
  • The ability to destruct items. val (name, age) = person
  • Higher-Order Functions, defined like "fun doStuff(body: Int -> T)): T". Which are much better than the groovy Closures IMO. (very similar to Scala's)
  • Type checks and smart casts are nice: https://kotlinlang.org/docs/reference/typecasts.html
  • 与伴生对象一样,scala也会try 从类中删除静电方法,kotlin也会try 相同的方法.
  • 密封类以限制继承(Scala也有类似的功能)
  • The "Nothing" subtype, where everything is a supertype of it. (another crucial concept in Scala).
  • when expressions for basic pattern matching: https://kotlinlang.org/docs/reference/control-flow.html

如您所见,它确实borrow 了Groovy以外的其他语言.他们试图精挑细选一些伟大的特征,以期创造出一门好的语言.当然,Groovy也有自己的优点.我只专注于柯特林有的东西,而不是签证-反之亦然

Another plus is, being made by an IDE maker, the compiler is very quick and has great IDE support. Not saying Groovy does not have good support, but my current project does take a long time to compile, and refactor method always assumes you are coding in a dynamic fashion.

我建议您try 一下Koan,让他们了解您喜欢该语言的哪些特性,以及它与groovy(https://github.com/Kotlin/kotlin-koans)的比较.

Kotlin相关问答推荐

在intellij中使用kotlin符号和floordiv

我可以更改方法中泛型类的类型参数边界吗?

在Kotlin中的嵌套when语句中,else块是强制性的吗?

为何Kotlin标准库中的AND和OR函数不能像&&和||一样进行短路运算?

在 Kotlin 中将两个字节转换为 UIn16

使用 LazyListScope 嵌套可组合项

为什么 android studio 不为所有安全参数生成代码?

Kotlin 中的部分类委托

找不到 androidsdk.modules

将 Completable 转换为 Single 的规范方法?

Hilt Activity 必须附加到 @AndroidEntryPoint 应用程序

Jetpack Compose – LazyColumn 不重组

Kotlin val difference getter override vs assignment

重复构建失败To use Coroutine features, you must add `ktx`......

用于代码生成的ANTLR工具版本4.7.1与当前运行时版本4.5.3不匹配

在 sharedPref.getString 中有一个默认值有什么意义?

kotlin中的全局可拓函数

Android Studio - java.io.IOException:无法生成 v1 签名

将协程更新到 1.2.0 后构建失败:META-INF/atomicfu.kotlin_module

如何在 Kotlin 中按字母顺序对字符串进行排序