Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Kotlin features are not available in statically compiled Groovy? [closed]

Tags:

kotlin

groovy

Kotlin and Groovy look as very similar languages with very similar features if we compile Groovy statically. Which features, apart from null safety, Kotlin has that are missing in Groovy?

like image 552
Jan Pomikálek Avatar asked Mar 13 '16 17:03

Jan Pomikálek


2 Answers

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.

Here are some differences I'm aware of

  • Static vs Dynamic: Since Groovy was designed as a dynamic language, and @CompileStatic, while a great annotation (I use it a lot), was added later. Its feature feels a bit bolted on, and it does not enforce people to code in a static manner. It's not usable everywhere (e.g. my Spock tests seem to fail to compile with them). Sometimes even with it on Groovy still seems to have some odd dynamic behaviour every now and then. Kotlin is 100% Static, and dynamic is not an option.

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/

  • Data classes - concise with a copy function (a bit like case classes in Scala)
  • 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
  • Companion Objects, in the same way Scala also tries to remove static methods from classes, Kotlin tries the same thing.
  • Sealed Classes to restrict inheritance (again Scala has something similar)
  • 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

As you can see it does borrow from other languages other than Groovy. They have attempted to cherry pick a number of great features in an attempt to make a good language. Naturally Groovy has its own goodness. I've only focused one what Kotlin has and not visa-versa

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.

I'd recommend you try out the Koans to get a feel for them to see which features of the language you like and how it compares to groovy (https://github.com/Kotlin/kotlin-koans).

like image 178
Bruce Lowe Avatar answered Nov 20 '22 12:11

Bruce Lowe


Kotlin designed as statically typed language, with great type system and other benefits of statically typed language. Groovy - in first place is a dynamically typed language, and only then - statically.

When you enable compile static in groovy you get just java with syntax sugar. On other side - Kotlin, in their type-system, have two types of references: nullable and nonnullable, so you can write code with less NPEs. If you are asking about only one feature - that's it.

Second great feature of Kotlin - it doesn't do any implicit conversions, on other hand - groovy implicitly converts double to bigdecimal and so on.

But kotlin has a lot other features, like smart casts, ADT (doc), type-safe builders, zero-cost abstractions and finally great IDE support.

Also i'm not sure about quality of Groovy's type-inference(in closures for example we need additional annotations, meh), but in Kotlin type-inference work's like a charm, without any annotations in every peace of language.

So statically typed compilation in Kotlin - first class citizen, in Groovy - not.

like image 5
Ruslan Avatar answered Nov 20 '22 12:11

Ruslan