375 points · by jakub_g · 9y ago · 233 comments
I wonder why they decided on these very mistakable names. Why not const/constant/cons/whatever else just as long it's distinguishable from each other?
let (mut x, y) = (1, 2);
x is mutable, y isn't.That is, let is the way that you introduce a new binding, always.
But this most likely is because Scala uses val/var.
val/var/const/whatever only describe the reference, not the value. It doesn't really make sense for that annotation to, say, swivel a collection between ImmutableList and MutableList.
Now, you might be right that it's confusing, this difference between reference mutability and value mutability. I see beginners struggle with it in Javascript's new let vs const all the time.
It does; if you don't use `let mut`, you can't mutate the variable at all, which includes the contents of a collection.
(Given that someone will mention RefCell if I don't, I'll add: "barring unusual trickery".)
That's nonsense.
1. You cannot implement immutable collections without `let` / `final`
2. The Java Memory Model has special visibility guarantees for `final` (which does propagate), making it really, really useful
3. Having the guarantee that a certain reference won't change is still useful even if the object referenced is a mutable ArrayList; e.g. ref = null
Remember recently on the Java mailing list there was a lot of bikeshedding concerning this point. I don't think it matters near as much as the discussion surrounding it thinks.
Oh, and finally, "const" has a specific meaning: compile time constants.
Maybe other people's brains work differently than mine, but I'm sure I will be slightly slowed down by this.
It might have appeared in earlier MLs as an extension.
I will echo what the others have said; it's not what I would choose, but in practice it's never been a problem when I'm writing Scala.
As a bit of an unreformed code golfer and a fan of ML-style syntax, I actually kinda like it myself; I was just listing a few things that are "considered questionable" (by some people) and present in both languages.
val array = new Array[String](10)
array.update(7, "hello")
array(7) = "hello" // simply calls out to updateval = value (constant)
What can be mistaken?
l => let
v => var
If you want to learn kotlin go to the official docs and tutorial, they are as good as the language:
http://kotlinlang.org/docs/reference/
And you don't need to install anything as you can try it in the browser:
My wife would leave me if she knew how much I love Kotlin, but I could never use it for work projects because we just could not justify expecting random inheriting developers to know Kotlin and accept its legitimacy. That constraint lifted in an instant. It's a political thing, but it really matters.
If I have an app with a codebase in a language that very few people know, that's a risk. What if my main developer leaves and I need to find someone else to continue working on the code base?
You can now bet that a lot of people will be picking up Kotlin as a result of the latest announcement.
Google only said they are going to collaborate more with JetBrains, which in my mind it translates in "we, developers, are not going to have incompatibility issues between kotlin and beta versions of the gradle plugin". (I want to highlight the word beta, because with stable versions it was weird you have any issue with kotlin).
Other factors to consider are tools support, how often interoprability with java libraries causes problems. Would be nice to hear from someone who has used it a bit about what the state of these is?
In the future I would use Kotlin for any Android need (obviously) and any time where I wanted to be lighter weight than Scala. As for native/JS use, I am waiting on the multiplatform story to materialize more before I have a strong opinion, but I would still go with Scala for how easy it is to develop multiplatform libs/progs w/ JVM/native/JS where I can easily have platform specific pieces. Kotlin is almost there[2], but not quite yet.
0 - https://github.com/cretz/asmble 1 - https://gist.github.com/cretz/2a49514b18914ef09b7c518db6db11... 2 - https://discuss.kotlinlang.org/t/abstraction-w-jvm-and-js-im...
First I should point out that many large Android apps are using Kotlin. There is a lot of momentum behind Kotlin on Android, that's why Google adopted it.
Tools support is really good. Jetbrains knows how to create tools and it shows. Using kotlin also shows you how great the android plugin for java is. Nothing major, but little features like shortcuts to create a string/drawable resource are sometimes missing in kotlin. Jetbrains is missing that gap though, and we should quickly reach parity now that the language is officially supported.
Interop with Java is equally great ! Both java & kotlin output bytecode, so they can dialog easily.
Some interop caveats : - sometimes calling java from kotlin is not idiomatic. However nowadays many libs are already offering kotlin bindings and again, this should improve.
- similarly, calling kotlin from java can be a bit awkward syntactically speaking.
- kotlin guarantees nullability behavior, but this can be broken at the interface with java in a couple of cases. If the java code lack @nonnull @nullable annotations, it is up to you to handle their nullability. Similarly, kotlin classes instantiated from java code can mess with nullability. If you data class contains a nonnull property and you instantiate this class with Moshi (a json parser), if the corresponding json field is missing you end up with a null field on a nonnull property.
So it is pretty rare, but you can still end up with some NPE.
In general, after bouncing between various desktop, mobile, and web-based technologies, I've come to the conclusion that any binding/event-based GUI system needs some sort of async-await or futures support to be truly workable. Otherwise, performing background tasks without locking up the UI involves way too much boilerplate and tends to blow up the architectural complexity of the application.
The mix of "hip" programming language constructs with unchanged Java verbosity feels weird to me. But if I were making an Android app, I'd definitely try to do it with Kotlin first.
(1..100).map({ it + 10 })
Doesn't seem so bad.
var map = {"a": 6, "b": 12}
[Whatever list type is most common/idiomatic in Kotlin] literals like: var list = [1, 2, 3]
etc.Plus list/map comprehensions like Python or Haskell, as you mentioned.
Not a big deal or anything. It's only a little bit of extra typing. And the syntax sugar they do add is definitely a massive step-up over Java.
I know it'd be hard to add the map literal syntax since it would clash with Java's normal array literal syntax. And I also realize that Java has a wide variety of container types for a reason, and Kotlin wants to be a subset of and not replacement for Java, so doing this may not make a lot of sense.
I guess on reflection, my complaints aren't really valid given that context. I've just had it ingrained in me to use special characters for basic container types, since Python and JavaScript were my first languages for many years.
(1..100).map { it + 10 }
For example Spring announced that Kotlin will be first class citizen with next major release. So things are definitely bright on the server side.
In another I used Spring Boot where Kotlin felt like a native in that environment.
I'm guessing if you are building something with ORMs or similar where Java Bean convention is needed, you might lack the beauty of immutability and data classes (there are workarounds though) but other than that most pieces drop in place without a hitch.
https://github.com/corda/corda
It's a distributed ledger/blockchain system being written for large, conservative banks and is around ~90%+ Kotlin code.
There's not much to say about it, it works fine, I wrote about it here:
https://www.reddit.com/r/programming/comments/6bqo7n/kotlin_...
When you have the option to use Java 8 without caveats (unlike in Android), the benefits of Kotlin aren't really _that_ visible.
I'm never going back to Java on Android though.
That's not a hard and fast rule. There are obvious inflection points. A 200 character long identifier is less readable than a 20 character identifier in almost all cases. A 1 character identifier is less readable than a 8 character identifier in almost all cases. Somehwhere between those extremes is the sweet spot, and it's likely somwhat different depending on person.
As for language keywords, as long as they are unique, unambiguous, and not likely to conflict with written code often, shorter is likely better since it's purely a matter of learning them when learning the language. Given those constraints, it's easy to see it's possible to be too short though (any single character keyword that is not context sensitive is likely a horrible choice).
> The fact that things are shorter to declare doesn't make it better.
This is debatable. In fact, shorter code could (but not always, of course) mean less room for error and more room to hold higher abstractions. Boilerplate is a less efficient use of cognitive energy.
val z = (1..9).map {it * 3}
.filter {it < 20}
.groupBy {it % 2 == 0}
.mapKeys {if (it.key) "even" else "odd"}
I'd like it more if it said it performed the functions in parallel. If it does this Automagically I'm sold. If it doesn't, is there an easy to use form that does do batch operations like map in parallel? (1..99).parallelStream()
.map { it * 3 }
.filter { it < 20 }
.collect(Collectors.groupingByConcurrent(Function<Int, String> {
if (it % 2 == 0) "even" else "odd"
}))
The result is of type ConcurrentMap<String, MutableList<Int>>A couple of notes about this code:
1. It uses the Java 8 streams API. I'm not sure it'll work on Android. The code you gave uses the Kotlin standard library extension functions, which is a different implementation of map/filter/etc using inline functions. When possible, use the Kotlin versions, which don't create new garbage or methods. But Kotlin's library is less powerful than the Java streams API, which can do automatic parallelism.
2. For this toy example the parallel code will be slower. There is a cost involved in automatically parallelising an operation that won't even begin to pay off until you're working with very large inputs.
3. Due to a bug in how Kotlin's type inference interacts with the Java 8 Streams.collect call, I have to specify the type of the lambda explicitly, which is a bit ugly. Especially as the IDE and compiler don't quite agree, so the IDE renders Function<Int, String> in grey, but if I remove it as suggested, I get a compile error. Such bugs are very rare (in fact Streams.collect is the only place I've seen it happen lately), but do highlight the fact that Kotlin has only been at version one for a year and is still maturing the level of stability you would expect from Java.
.groupBy {if (it % 2 == 0) "even" else "odd"}> val notPositive = not {it > 0}
This idiom took me by surprise. It's really quite lovely, and now I'm disappointed C# doesn't include it.
I hope kotlin native is as good as scala native. I don't care about kotlin vs Java, but kotlin vs scala native is HUGE to me.
Swift is similar in that iOS developers don't have to use Objective C any more, but actually looks like a nice language in its own right.
I think although Kotlin is going to be an improvement for Android development, it's not going to change much unless Google drastically simplify the actual Android API. You're still going to have to deal with this shit: https://github.com/xxv/android-lifecycle
Java : Kotlin :: JavaScript : TypeScript
This seems like a good idea.
if let person = selection?.organization?.owner {
setTitle(person.name)
setImage(person.image)
} val person = selection?.organization?.owner?.let {
setTitle(it.name)
setImage(it.image)
it
} val person = selection?.organization?.owner?.also {
setTitle(it.name)
setImage(it.image)
} if let person1 = dashboard?.owner, person2 = selection?.organization?.owner {
person1.cloneProperties(person2)
}What do I have for kotlin?
Kotlin has a really nice and simple syntax compared to Scala, which has way to many features.
Kotlin's standard library is tiny. It consists mostly of extension functions and aliases over the Java standard library. For instance kotlin.String is just an alias to java.lang.String, it's exactly the same object. Kotlin's lists are just more type safe views over java.util lists. Much of Kotlin's standard library is in fact inlined and then optimised, so using it doesn't even create function calls in your bytecode.
So whilst Kotlin does technically have a standard library, to do anything practical you'll be using Java's. And that's one reason why we like it. No duplicate standard library to learn.
> "Function arguments are specified in brackets after the function name."
No they're not! They're specified in PARENTHESES after the function name.
I think that's a good thing, you end up with a language that feels pragmatic and powerfully practical.
everyone everywhere hates android dev work. so they think that it must be java. no, java sucks, but kotlin sucks just the same. the problem is the android ecosystem as a whole. the ever changing apis. etc.
wasting time on java vs kotlin is absurd with so many other real problems.