Live data from Hacker News

From Java to Kotlin and Back Again

allegro.tech

141–150 of 199 posts

Re: From Java to Kotlin and Back Again

#141

Earlier quoted context omitted.

A bit too far I'd say. I'd probably happily work with him.

I have seen his other posts and he is a smart guy. No idea what bit him with Kotlin though...

Probably drinking too much koolaid from the AbstractBeanContainerMappingClassFactory... Sadly, this might be a classic example of the the Blurb paradox.

Re: From Java to Kotlin and Back Again

#142
post #99
post #45

Is it me, or does the article feel like it is written by someone who didn’t actually bother to learn Kotlin. Instead they learnt the bare minimum syntax and tried to write Java in Kotlin. The author complains a lot about how Kotlin is different from Java. Err.. it is a new language — it is supposed to be different — otherwise why bother? > I have my favorite set of JVM languages. Java in /main and Groovy in /test are…

> The author complains a lot about how Kotlin is different from Java. Err.. it is a new language — it is supposed to be different — otherwise why bother? Kotlin advocates try to have it both ways. If it's a full new language, to be evaluated as a full language, then why would you adopt it when it's missing important features compared to Scala? The narrative is that it's a set of small enhancements to Java that are ea…

> then why would you adopt it when it's missing important features compared to Scala?

A useful IDE, much faster compile time, lower cognitive load in daily usage.

Re: From Java to Kotlin and Back Again

#143
The author wrote a lot about nullable types, but didn't mention a very useful feature of the Kotlin compiler: it understands several (IIRC, more than a dozen) third-party Java nullability annotations. It treats Java types annotated with @NotNull or equivalent as T, types annotated with @Nullable as T?, and only types with neither are treated as T!.

And on the other direction, the bytecode generated by the Kotlin compiler also has these Java nullability annotations (it uses the org.jetbrains annotations package), so for instance Dagger2 can know that a type is nullable from the presence of the @Nullable annotation.

If your code already uses @NotNull/@Nullable everywhere, introducing Kotlin to your code base becomes easier. As a bonus, IDEs can sometimes use these to warn you of potential null pointer mistakes in your code.

Re: From Java to Kotlin and Back Again

#144
post #45

Is it me, or does the article feel like it is written by someone who didn’t actually bother to learn Kotlin. Instead they learnt the bare minimum syntax and tried to write Java in Kotlin. The author complains a lot about how Kotlin is different from Java. Err.. it is a new language — it is supposed to be different — otherwise why bother? > I have my favorite set of JVM languages. Java in /main and Groovy in /test are…

> > I have my favorite set of JVM languages. Java in /main and Groovy in /test are the best-performing duo for me. [...] we decided to give Kotlin a try.

> The opening lines set the tone for the article

He also wrote:

> we decided to stick with Groovy in /test (Spek isn’t as good as Spock).

If they were going to switch from the legacy language for /main code (i.e. from Java to Kotlin), why didn't they also switch from the legacy language for /test code too (i.e. from Apache Groovy to Kotlin) instead of dismissing it with "Spek isn't as good as Spock". And if they use Gradle, why not switch from Groovy to Kotlin for build scripts also.

Re: From Java to Kotlin and Back Again

#145

Earlier quoted context omitted.

>Why is ":" problematic exactly? It's more concise. Anyone, including non-java programmers, can make an educated guess at what the "extends" keyword does to a java class. The colon operator is contextless. Unless someone tells you or you've coded in c++/c#, you can't know for sure what it means without googling it. Here's a case in point, my biggest bugbear with the nim documentation. When you first see nim code, you…

> The colon operator is contextless It has a type after. Always. Even assuming that's not enough for it to click, that Google search took all of 3 seconds and you are now typing ":" over "extends" thousands upon thousands of times. If you honestly thing one of those takes more time than the other, you haven't really thought this through. This is a silly argument.

>and you are now typing ":" over "extends" thousands upon thousands of times

That's blatant hyperbole, and also a worthless argument. How many times have you typed "int"? Should we replace it with a symbol? Maybe go back to space-cadet keyboards[1]?

J is a language built around symbols for maximum programming speed. It's not an esolang, it's real and usable, and recently updated. Here's a very simple J function:

result=: {&((65 97+/~i.2 13) |.@[} i.256)&.(a.&i.)

Very efficient, minimum keystrokes. Can you tell me what it does?

Fundamentally, programming languages are for people, not computers. Otherwise, we'd all be coding in assembly. Keystrokes are /not/ the expensive or dangerous part of programming, we shouldn't be optimising our languages to reduce them. Optimising languages to reduce miscomprehension is the future, that's where expensive developer time and business risk is concentrated.

Preferring to type a colon instead of the word "extends" is, bluntly, the sort of thing you solve with a custom autohotkeys file.

In any case, it's a minimal gain either way with the : operator, that kind of decision won't make or break any language. But this is the internet and if we're going to argue over something so trivial, I'm going to argue that on the most nitpicky level, "extends" is clearly a better choince than ":".

[1] Space-cadet keyboards are really cool

Re: From Java to Kotlin and Back Again

#146
post #45

Is it me, or does the article feel like it is written by someone who didn’t actually bother to learn Kotlin. Instead they learnt the bare minimum syntax and tried to write Java in Kotlin. The author complains a lot about how Kotlin is different from Java. Err.. it is a new language — it is supposed to be different — otherwise why bother? > I have my favorite set of JVM languages. Java in /main and Groovy in /test are…

This was my first take on swift as an Obj-C dev back in 2014. I did an app extension in swift, basically just trying to write Obj-C code in swift (force cast and force unwrap all the things!). I didn't like it, I felt like it got in the way, and probably would've given a list of things like the author that are just lang differences.

Needless to say after my 2nd round with swift it clicked, and then clicked again and again in different ways as the months rolled on. Never looked back.

Re: From Java to Kotlin and Back Again

#147
post #134

Earlier quoted context omitted.

> Re immutable: If you're doing functional why does this matter? "true" immutable vs. just an immutable view should only matter if the object is retained by the function it's passed to, but now we're talking object state rather than pure functions. For the same reason that being able to write val rather than var matters. With perfect discipline you can write pure functional code in any language, but in practice (and…

You can do something like this: class MyTransaction { fun begin() {} fun commit() {} } fun MyTransaction.query() {} fun MyTransaction.update() {} inline fun transaction(tblock: MyTransaction.() -> R): R { val t = MyTransaction() t.begin() try { return t.tblock() } finally { t.commit() } } fun test() { // update() doesn't compile, no such method val hello = transaction { query() update() query() "Hello" } println("$he…

I must admit I don't entirely understand how your approach works, but it seems like it requires all the functions you want to use in transactions to live on MyTransaction (even if only as extension methods)? I want to be able to write normal functions including quite high-level ones that take some business objects and return MustHappenInTransaction[SomeOtherBusinessObject], and pass these return values through generic methods that don't know anything about transactions specifically.

Re: From Java to Kotlin and Back Again

#149
post #66

"Kotlin changed the extends keyword into the : operator, which is already used to separate variable name from its type. Back to C++ syntax? For me it’s confusing." Not only C++; C# also. It's not very hard to confuse the author, is it :) I understand it's different from Java he's used to, but that doesn't mean one can hold every such difference against Kotlin. Not being Java isn't in and of itself a fault of a langua…

Actually, the colon is consistent here: `val x: Integer` means x isA integer; `class Dog(): Animal()` means Dog isA animal. They mean the same thing within their respective contexts.

Re: From Java to Kotlin and Back Again

#150
post #17

Earlier quoted context omitted.

steep learning curve does seem laughable. If you know Java, you can be start writing kotlin after a couple of hours with the kotlin koans. My main pain point with kotlin is that I generally have a good idea of how my code will look like as bytecode (like this feature will need an intermediate object, so let's use this instead in this very big loop). In kotlin, it is a bit more of an uncharted territory, even with the…

I've had the same complaint before, for sure--when writing very perf-conscious, GC-sensitive stuff I still might hop over to Java because I do understand its exact behaviors a little better. But, as you mentioned, the bytecode viewer helps a lot.

I should have mentioned that in both cases, it is a bit of a pipe dream to think you can envision what the compiler will churn out.

Especially with one that is constantly evolving like kotlinc.

Some of the bad practices of 5 years ago have devolved into cargo cults and the compiler can actually handle these cases fine now.

So I guess that this is not that great of a reason to choose either Java or Kotlin.

In both cases, if performances are critical and lacking, you will have to whip out systrace and look at the bytecode.

Post reply on HN