78 points · by venturis_voice · 9y ago · 203 comments
E.g. already there's no way to reuse logic between code paths that operate on Kotlin nullable types, Java Options (needed for compatibility with Java streams or more generally other JVM libraries), or rust-style Result types (for which there are several Kotlin libraries already, because it's a very useful concept and the language mostly has the facilities to support it), even though all three behave almost identically and you would often want to do the same thing in all three cases. But since the language won't let you represent what these types have in common, your only option is to copy/paste the code.
1. Just because a while loop subsumes the functionality of a for loop doesn't make the for loop useless. 2. Algebraic data types are technically subsumed by inheritance and predicate dispatch [1]. That doesn't make ADTs useless, because often you don't want to lug the extra baggage of the more powerful model around with you, especially for simple use cases.
The other problem is that "more general" is in the eye of the beholder. For example, to a Smalltalk programmer, most use cases for monads look pretty primitive, as they're limited to user-defined control flow on a sequence of operations, nothing more complicated; they'd be implemented in Smalltalk as operations on arrays of blocks. Keep in mind that Smalltalk doesn't even have if statements in the language proper: `ifTrue:` and friends are methods of the Boolean class. Similar things apply to other control structures, such as `whileTrue:`, not to mention that plenty of data types offer their own control structures. And the continuation monad in the article looks a lot less impressive compared to Seaside [2], one of the original continuation-based webservers. (I'll also add that the reason for `.then(..)` or `async/await` is often that the underlying languages do not support continuations, not that they model the non-existent continuations imperfectly.)
[1] See "Predicate Dispatch: A Unified Theory of Dispatch" by Michael Ernst, Craig Kaplan, and Craig Chambers, https://homes.cs.washington.edu/~mernst/pubs/dispatching-eco...
I think here you're assuming that a feature that allows more possibilities to be expressed subsumes one that doesn't, and I would disagree; being able to clearly restrict the possibilities is important too. As an exaggerated example, replacing all the language builtins with a single builtin that took a parameter to say which builtin it was wouldn't be an improvement. (At the same time being able to talk about different builtins in a uniform way - e.g. being able to use a builtin function as a value - is a useful generalisation - I see monads as more like this).
> to a Smalltalk programmer, most use cases for monads look pretty primitive, as they're limited to user-defined control flow on a sequence of operations, nothing more complicated; they'd be implemented in Smalltalk as operations on arrays of blocks.
The Free monad is essentially that (it's a type-aligned sequence of things that look like Kleisli arrows, which is pretty much the type-safe equivalent of an array of blocks), and it's well-known that you can emulate any monad that way (specific concrete monadic types are nice to have as well though, for the cases where you do want to talk about that specific type). If you're saying that monads are something banal rather than something complex and impressive then I completely agree.
> Keep in mind that Smalltalk doesn't even have if statements in the language proper: `ifTrue:` and friends are methods of the Boolean class. Similar things apply to other control structures, such as `whileTrue:`, not to mention that plenty of data types offer their own control structures.
Sure - this is how control flow tends to work in monad-oriented languages too, with constructs like ifM . I don't see a conflict between this style and what I see as the good aspects of Smalltalk at all; rather I see this style as a way to get those aspects while having type safety as well.
The point here is not that Smalltalk is better or worse; the point is that Smalltalk's approach can be seen as more general. In other words, it's a matter of perspective as to what is considered to be a sufficient amount of generality.
Types are orthogonal to that (see Strongtalk).
Edit: To clarify something:
> The Free monad is essentially that (it's a type-aligned sequence of things that look like Kleisli arrows, which is pretty much the type-safe equivalent of an array of blocks), and it's well-known that you can emulate any monad that way (specific concrete monadic types are nice to have as well though, for the cases where you do want to talk about that specific type). If you're saying that monads are something banal rather than something complex and impressive then I completely agree.
Partly that (I've said before that I much prefer the F# terminology of "computation expressions" to get the purpose across), but my larger point is that this would be considered to be a very specific and limited application to a Smalltalk programmer. Again, it's not about Smalltalk being better or worse, it's about "can be generalized" as a matter of perspective.
override def covariantInstance[C]: Applicative[C =>: ?] =
new Applicative[C =>: ?] with SndCovariant[C] {
def point[A](a: => A): C =>: A = arr(_ => a)
def ap[A, B](fa: => (C =>: A))(f: => (C =>: (A => B))): (C =>: B) = <<<(arr((y: (A => B, A)) => y._1(y._2)), combine(f, fa))
}
More importantly, as I bring people on to the team, I don't have to try and explain it to them. Scala was an interesting language, and I'm glad I spent some time digging into it, but its compromises around the JVM and java source compatibility and supporting every programming paradigm it could find makes it an unproductive language in my experience. class applicativeCovariantInstanceStringString<C>: Applicative<EqualsGreaterThanColon<C, String, String>> {
fun point(a: () => String): EqualsGreaterThanColon<C, String> = arr(_ => a())
fun ap(fa: () => EqualsGreaterThanColon<C, String>))(f: () => EqualsGreaterThanColon<C, String => String>): EqualsGreaterThanColon<C, String> = LessThanLessThanLessThan(arr((y: Pair<String => String, String>) => y.first(y.second)), combine(f, fa))
}
class applicativeCovariantInstanceStringInt<C>: Applicative<EqualsGreaterThanColon<C, String, Int>> {
fun point(a: () => String): EqualsGreaterThanColon<C, String> = arr(_ => a())
fun ap(fa: () => EqualsGreaterThanColon<C, String>))(f: () => EqualsGreaterThanColon<C, String => Int>): EqualsGreaterThanColon<C, Int> = LessThanLessThanLessThan(arr((y: Pair<String => Int, String>) => y.first(y.second)), combine(f, fa))
}
class applicativeCovariantInstanceStringFloat<C>: Applicative<EqualsGreaterThanColon<C, String, Float>> {
fun point(a: () => String): EqualsGreaterThanColon<C, String> = arr(_ => a())
fun ap(fa: () => EqualsGreaterThanColon<C, String>))(f: () => EqualsGreaterThanColon<C, String => Float>): EqualsGreaterThanColon<C, Float> = LessThanLessThanLessThan(arr((y: Pair<String => Float, String>) => y.first(y.second)), combine(f, fa))
}
class applicativeCovariantInstanceIntString<C>: Applicative<EqualsGreaterThanColon<C, Int, String>> {
fun point(a: () => Int): EqualsGreaterThanColon<C, Int> = arr(_ => a())
fun ap(fa: () => EqualsGreaterThanColon<C, Int>))(f: () => EqualsGreaterThanColon<C, Int => String>): EqualsGreaterThanColon<C, String> = LessThanLessThanLessThan(arr((y: Pair<Int => String, Int>) => y.first(y.second)), combine(f, fa))
}
// and so on for all other pairs of types
I mean, I'm not going to defend whatever library you got that from, because I don't think EqualsGreaterThanColon or LessThanLessThanLessThan are very good names (whether you write them with letters or with symbols), but I don't think restricting the language actually makes it any better.Part of designing a good abstraction is the interface communicating usage and intent, which should be tailored to the problem domain and the audience.
for {
a <- list1
b <- list2
} { do something}
for an imperative for loop, and for {
a <- whatever1
b <- whatever2
} yield { ... }
for the general comprehension. In Python you have to put the equivalent of the yield right at the start, which is just so different from how everything else in the language works.https://en.wikipedia.org/wiki/Set-builder_notation#Sets_defi...
Yeah, I think that was a huge mistake, because the most common case is one-level and if you have one level the syntax is just the reverse of a regular for, but in a two-level comprehension (the rare case) you have to remember that it's not a reversal, it's a cyclic permutation.
i.e. most of the examples you see look like this, and these two are equivalent:
[y for i in x]
for i in x: y
and then occasionally you see a comprehension that looks like: [z for j in y for i in x]
so you'd expect this to be equivalent to for i in x: for j in y: z
but it isn't, it's equivalent to for j in y: for i in x: z
which is really hard to remember.I don't think so. Java has been used successfully in large codebases, probably more so than Scala, and Kotlin, like Go, is a language following Java philosophy.
> I think Kotlin has literally every one of those ad hoc solutions built into the language, but stubbornly avoids making the generalisation.
1. Ask yourself why does it stubbornly avoid making such generalizations. There are very, very good reasons. 2. Kotlin has delimited continuations, which express anything monads can (with a minor, unimportant caveat, which has to do with the particular features of Kotlin coroutines), in a way that blends much more nicely with imperative languages features that abound in both Kotlin and Scala, and composes much more nicely than monads (although that requires a feature currently absent an Kotlin's coroutines, but the feature is largely unnecessary for the kind of code Kotlin aims at).
> already there's no way to reuse logic between code paths that operate on Kotlin nullable types, Java Options
That's not really accurate. Kotlin does this blending at least as well if not better than Java. Java options are not really intended to be used in the same way they're used in Scala.
> or rust-style Result types (for which there are several Kotlin libraries already, because it's a very useful concept and the language mostly has the facilities to support it)
Those are not idiomatic in Kotlin, and some (like me) think that the Kotlin idioms are better, at least for the kind of code it aims to support.
> But since the language won't let you represent what these types have in common, your only option is to copy/paste the code.
I don't think it is a language's job to support all idioms out there, even if they can be useful in some circumstances. Languages following the Java philosophy -- as opposed to Scala -- aim to be at least somewhat opinionated, and encourage similar coding patterns in all cases, even if some idiom can be marginally better suited in some specific case. It's a tradeoff between code homogeneity and being "multi-paradigm", which can only be settled by the values of the language. There is no right or wrong answer here. Kotlin's values are very, very different from Scala (even if many features are shared), which is why Kotlin was created in the first place. Its designers wanted a modern language in the Java tradition (I know that from directly talking to them), not in the Scala tradition.
Java didn't have anything like as many ad-hoc language-level features as Kotlin has - checked exceptions are the only thing that's really comparable, and their reputation is decidedly mixed (note even Kotlin hasn't included them).
> Kotlin has delimited continuations, which express anything monads can (with a minor, unimportant caveat, which has to do with the particular features of Kotlin coroutines)
They can, but only in the same sense that an untyped language can express anything that a typed language can. They can express any effect but they can't express restricted subsets of effects. You could presumably write a continuation-based equivalent of traverse (though I'd be interested to see what it looks like in practice), but you can't give it the level of type safety it should have, and you can't write sequence when your effects aren't representable.
> Kotlin does this blending at least as well if not better than Java.
Not convinced; nulls aren't integrated as tightly into the language in Java (e.g. no elvis operator) and you don't have to deal with platform types. You can write Option-oriented code without it being a second-class citizen, though admittedly the second-class citizen experience in Kotlin may not be so much worse than the first-class citizen experience in Java.
> I don't think it is a language's job to support all idioms out there, even if they can be useful in some circumstances.
Yes and no. A language doesn't have to support everything out there, but putting together any of the features it does support should work the right way; it's not at all clear that this is the case for kotlin . And certainly when you end up copy-pasting code because you're using the same logic but the language doesn't let you express how it's the same, that's a problem with your language.
That is not the case. Sometimes language designers and maintainers _deliberately_ avoid language constructs that allow for more generic code. More generic is not always better, definitely not when you have to read and understand the code, and find a bug in it, for example. The more layers of abstractions you have, the harder it is to construct a model of it in your mind.
(An example of this approach is Erlang, where parameterized modules were introduced as an unsupported, "beta" feature at some point, then a few major versions later they were removed. Why? Take this opinion, for example:
"I would gladly reconsider my somewhat harshly critical position on parameterized modules if shown a clear example of how their use improves the readability or expressiveness of code. Until then I would urge that they didn't become an official part of the Erlang language."
http://erlang.org/pipermail/erlang-questions/2010-February/0...)
This is not true. E.g., see my blog post, http://blog.paralleluniverse.co/2015/08/07/scoped-continuati..., with references to work by Oleg Kiselyov on typed continuations. Having said that, restricting effects is one of those solutions in search of a problem, at least at the moment. It is very questionable whether this has any substantial benefit, and the Java philosophy calls for adopting only features that have proven their (significant!) worth. On top of that, Scala can't restrict effects, either.
> You can write Option-oriented code without it being a second-class citizen
Which, BTW, Java discourages. Option is mainly recommended for use in functional composition in streams. For general null-safety, Java has pluggable nullability types (and many other useful pluggable types), similar to Kotlin's built-in nullability types. See https://checkerframework.org/. Unfortunately, it's not yet seeing much adoption, largely due to the relatively low-key marketing of Checker and the fact that it's been not too user-friendly until quite recently. However, this is the recommended solution, e.g. in the Java tutorial: https://docs.oracle.com/javase/tutorial/java/annotations/typ....
> putting together any of the features it does support should work the right way; it's not at all clear that this is the case for kotlin
Right, and certainly Kotlin has not yet proven itself (and neither has Scala), but in my opinion, this is quite clearly not the case for Scala. E.g., monads and the imperative control structures in Scala simply do not compose. You must commit to one or the other.
> And certainly when you end up copy-pasting code because you're using the same logic but the language doesn't let you express how it's the same, that's a problem with your language.
Not when that's required for non-idiomatic code. And, BTW, I believe that the pure functional paradigm in general suffers very strongly from this in a way that's very essential to the core of the paradigm (because typed monads introduce a dependence of code structure on a nominal type, i.e., the same computation can be expressed using different monads -- or no monads at all -- in a way that is non-composable or requires awful monad transformers).
The group’s pretty quiet at the moment whilst we’re building up the community but stick with us as we’re aiming to grow it rapidly.
https://publicslack.com/slacks/https-venturisvoice-slack-com...
The group’s pretty quiet at the moment whilst we’re building up the community but stick with us as we’re aiming to grow it rapidly.
https://publicslack.com/slacks/https-venturisvoice-slack-com...
Who cares about metaphors explaining the nature of a Monad, or type theory yadda yadda. This article shows precisely the problem Monads solves for real world programmers. Very few other Monad tutorials do this, and none as well as this article.
Well done and much thanks to the author!
You do not need monads everywhere. Once you have a hammer, every problem looks like a nail. Monads are as ad hoc as any other solution listed there: that is, not ad hoc at all. These are all choices made by designers of the language. A generalized solution (shoving monads everywhere) may as well be much worse than a specific solution to a specific problem.
BTW. List comprehensions are amazing as long as they are not written with Pythonic syntax, but that's a minor complaint.
Clearly they're not ad-hoc, since they can be used for all of those examples and more, whereas each of the ad-hoc solutions has to be implemented separately as a language-level feature.
> A generalized solution (shoving monads everywhere) may as well be much worse than a specific solution to a specific problem.
I think this is true in the small. Of course a dedicated syntax like async/await is going to be better suited to that specific problem than reusing a general-purpose solution like do notation. But once you're working on a larger codebase, being able to support new effects in a plain old library rather than having to have them built into the language by the language maintainer (or, worse, hacked-in by bytecode manipulation configured through annotations or something), and being able to generalise over multiple different effects or different representations of the same effect, is much more important.
(I do hold out hope that it's possible to get the best of both worlds by having dedicated syntax but only as lightweight sugar over the general-purpose syntax)
I won't hold my breath waiting for large projects coming out of Haskell.
If anything, boring and ad-hoc seem to win in the "large codebase" space. It's enough to look at all the projects that have come out (and keep coming out) of Java, and at the projects that are now coming out of Go and Rust. And then at the barren wasteland that is Haskell (or other languages so proud of their monads).
> they can be used for all of those examples and more
There's a probably a reason hardly anyone uses them as a language-level feature.
For JS, you can use all you favorite front-end frameworks like react (here's an example by JetBrains themselves https://github.com/Kotlin/kotlin-fullstack-sample) or cycle. And you can import typescript definitions.
Also first class IDE integration.
So yeah, bright future is an understatement.
I guess clojure/clojurescript and scala.js are somewhat similar however I feel like Kotlin has an insane amount of momentum behind it right now.
The JNV / JS / Native targets are also really promising, though I haven't played around with them much. The JS and Native runtimes are a bit limited since they don't include the Java standard library, from what I can tell.
I think that they ship with the Kotlin standard library which is close enough.
> various lisps have been doing it for decades
There's an insanely long road from having a prototype of a language to having a production ready thing. There isn't a lisp where I could achieve the same experience.
It isnt a mature enough language to be trusted so soon, no body has a sufficient enough industry grade experience with it. Especially with large code bases.
C/C++ can do that if you accept Emscripten.
Java can do that if you accept GWT.
Though I think what really sets it apart (on the web side) is that it interops pretty well with other js libs.
I think what sets it apart is the interopability with it's current targets. Really slick
Edit:fixed typo
Lisps have been doing this since forever (including the now defunct LinJ for Java which compiled into code that was very often mistaken for hand-written Java).
Hell, Emscripten takes everything LLVMable and gives it that ability.
> This is unprecedented.
Only for sufficiently narrow "this".
Don't get me wrong, I like Kotlin, but Ceylon did the multi platform thing much better.
They have real platform modules, built into the language (not just a build tool trick), and a language specification that was written from the start with multiple platforms in mind.
But it never took off (yet), and Kotlin's support is good enough, so it will have to do I guess :).
The group’s pretty quiet at the moment whilst we’re building up the community but stick with us as we’re aiming to grow it rapidly.
https://publicslack.com/slacks/https-venturisvoice-slack-com...
With Kotlin you'll be running compiled code. If you wanted to right now though, you could (should be able to?) use Kotlin to build a React Native app with it's just target.
[1] https://blog.jetbrains.com/kotlin/2017/04/use-kotlin-with-np...
I like kotlin for several reasons but this one isn't on the top of my list. If you like cross-platform development, you might want to check out haxe.
At its peak, Eclipse released Xtend. At that point, Eclipse was the IntelliJ of today, everyone assumed Xtend would be the next Java with full support of Eclipse tooling. Today, well, anyone remembers it? But its still there, check the features: http://www.eclipse.org/xtend/documentation/index.html.
You will see thousands of articles on kotlin vs java, but only a handful on kotlin vs xtend, because of the cycle.
Groovy. Same cycle, same result.
Javascript has a large share of syntactic forks. All of them had this hype-cycle. Coffeescript was "de-facto way" to write cross-browser eloquent javascript at one point.
There are few lessons from history: Syntactic language forks are what they are - forks. It takes equal amount of tooling and expertise to maintain them. When the next two versions of Java releases, the hype cycle will again turn tides. "X is the better Y", "Groovy is the better Java" - never, there is only one Java, there is no better Java.
The best approach is to make the language itself extensible. Look at ES-next and babel. Every language addition is a proposal, and proposals can be implemented as language plugins with stages 3/2/1/0. This effectively rendered most syntactic forks almost meaningless and unnecessary. Have an idea? Extend to the language directly! The most important part - tooling - which is eslint, AST parsing, intermediate representations, code formatting, etc - have been shifted to work on top of language plugins. Now some of you will talk against babel, and how different projects use different babel settings - but given all the history, babel is the best way to pragmatically evolve a language we've ever had so far.
Java has a longer mature JSR and language proposal process, just like ES. I really regret that Jetbrains decided to create yet another language fork, while being in the position to create the next babel for java.
Will Kotlin be different from all the previous cycles? Only real 2020 will tell, but until then, the only reference available is history.
Even the article quote something around those lines: “The syntax for Kotlin, like Apple’s Swift, is clean and modern. The reaction amongst developers learning Kotlin is not dissimilar to the initial experiences with Ruby years ago. The syntax is so intuitively designed that even without any previous exposure it’s almost possible to guess it.”
The question would be: Am I missing something? Is syntax the only reason that makes people excited about it? or are there any new abstractions/ideas that are exclusive for Kotlin?
But that's just one example and a subjective one at that. Not everyone will feel the same way and for those who don't there's no compelling reason to 'start using Kotlin' - if what you have works then stick with it.
There's also Kotlin native and Kotlin JS - allowing Kotlin code to be compiled to native binaries as well as to javascript is pretty compelling and will (imo) pay dividends. Both are still very much in development though.
That with combination of precise refactorings, unlike in case or ruby. And without unnecessary complexity of scala.
Programming languages are basically nothing but syntactic sugar over machine code.
Beyond that, it's nothing that you couldn't cobble together without the standard Guava + Commons + Lombok + Quasar/EASync etc. libraries and plugins that you'd use on every project written in Java, as well as that terseness you mentioned that makes people excited (see Kotlin data classes [1] vs Lombok data classes [2], if/try returns values and destructuring [5], coroutine support for lightweight threading and generators [6], null safety w/o optionals and smart casting, etc. [7]).
In my opinion, it's hard to know what value those features provide without actually trying them out yourself. If you want to be certain that you're not missing something, I'd suggest writing up a little script to try out some of its features.
edit: Just noticed the other responses on this thread. It's also very useful for starting fights with Scala developers, apparently.
[1]: https://kotlinlang.org/docs/reference/data-classes.html
[2]: https://projectlombok.org/features/Data
[3]: https://kotlinlang.org/docs/tutorials/javascript/kotlin-to-j...
[4]: https://kotlinlang.org/docs/reference/native-overview.html
[5]: https://kotlinlang.org/docs/reference/control-flow.html
Really excited to see how Native progresses as well. I've got a handful of projects what would benefit from having the 90% core written in Kotlin with some JVM and Native portions plugged in, ultimately meaning the library is available on both JVM and Native.
A language isn't a "real" language unless you can live in it and be blissfully ignorant of anything else. I don't need to know C in order to use Go, or Python. I don't need to know Ruby in order to use JavaScript. But it'll be relatively impossible to write code in Kotlin without interacting with Java constantly. Which means context switching and maybe even bugs.
For web services I use Sparkjava but I'm thinking of moving to Javalin or http4k because Sparkjava has really slow release cycles.
JSON is either Gson or Jackson. I prefer Jackson just because I know it better but they're both great.
I use JDBI for database work but I don't have too much of that in my backend right now. GRPC for cross service communication.
Back in 2015/2016 I used Vert.x a bunch but I really didn't like the async programming model. YMMV.
Generally I stick to using well known / common libraries written for Java and then later look for a Kotlin version if I have time to dick around a bit. I think this is what I love the most about Kotlin is the great interop with existing Java libraries.
All whilst avoiding adding clever/stupid things (cf. Scala, Go, Rust).
The group’s pretty quiet at the moment whilst we’re building up the community but stick with us as we’re aiming to grow it rapidly.
https://publicslack.com/slacks/https-venturisvoice-slack-com...
Scala was much better than Java but now the gap has closed and while its still nice to work with, I feel most applications would be better to stick to mainstream Java.
Kotlin is interesting but after Scala experience I'm reluctant to invest all over again. Is it worth it?
To answer your specific question, however, if you're doing Android development it's worth it :) otherwise, it's up to you/your specific situation.
[1]: https://kotlinlang.org/docs/reference/type-safe-builders.htm...
If you want to, Kotlin can be practically Java without the over-verbosity and some nice extra features. In time you'll be writing less Java-Kotlin and more Kotlin-Kotlin, if you get what I mean. To me the syntax and structure is so resembling to Java that I haven't really had any confusions at all, most of my gotta-read-the-docs moments involve the concepts that you can use that aren't present in Java (along with the `when` version of `switch` which I've grown to love).
Google made their Java fork, for Java 7 and 8 they only cherry picked features, even after migrating to OpenJDK.
The newly introduced bytecodes are not supported and the situation will only get worse with Java 9 and the new OpenJDK release schedule.
Specially given the lack of updates across the Android eco-system.
So only something like Kotlin allows to enjoy modern JVM languages while targeting Android.