9 ms·
As a Scala programmer, Kotlin always feels like a language that frustratingly almost gets it, but is infuriatingly unwilling to listen to experience of what wor
by lmm 9y ago
As a Scala programmer, Kotlin always feels like a language that frustratingly almost gets it, but is infuriatingly unwilling to listen to experience of what works in large codebases. Look at the examples on https://philipnilsson.github.io/Badness10k/escaping-hell-with-monads/ https://philipnilsson.github.io/Badness10k/escaping-hell-wit... - I think Kotlin has literally every one of those ad hoc solutions built into the language, but stubbornly avoids making the generalisation. In small examples ad hoc solutions look great, but to succeed in large codebases a language needs a generality and consistency that Kotlin lacks.
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.
- pron 9y ago> but is infuriatingly unwilling to listen to experience of what works in large codebases. 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.
- lmm 9y ago> Java has been used successfully in large codebases, probably more so than Scala, and Kotlin, like Go, is a language following Java philosophy. 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.
- haspok 9y ago> 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/049637.html http://erlang.org/pipermail/erlang-questions/2010-February/0...)
- bitL 9y agoSome of those examples trade development hell with debugging hell (e.g. list comprehensions). While working with Python's list comprehensions, I often find myself writing loops instead of super-complicated incomprehensible list comprehensions. So hijacking your statement, those ways work well for simple use cases, not so much for real-world ones.
- nerdponx 9y agoDon't understand why people don't like nested for-loops. I love list comprehensions, but I honestly would not consider one and abstraction, generalization, or improvement of the other.
- rb808 9y agoAgreed, the sheer usefulness and joy of having a legible stack trace with the ability to set debug points is invaluable.
- lmm 9y agoI find Python's list comprehensions unusable because they're backwards from how I'd expect. Thankfully in Scala the syntax is almost exactly the same; you write 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.
- vram22 9y agoThat's an interesting point I had not thought of before. For me, it does not make them unusable, but tastes differ. Also see my sibling comment.
- kazinator 9y agoWhoever designed the comprehension syntax for Python was perhaps enamored of the "such that" math notation { expr-with-vars | constraint-predicates }. https://en.wikipedia.org/wiki/Set-builder_notation#Sets_defined_by_a_predicate https://en.wikipedia.org/wiki/Set-builder_notation#Sets_defi...
- dmitriid 9y agoThat particular link is extremely highly opinionated. 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.
- lmm 9y ago> 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. 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)
- dmitriid 9y ago> I think this is true in the small. > once you're working on a larger codebase 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.
- edem 9y agoI think it us the other way around. Scala code tends to become an untangleable mess over time while Kotlin stays simple since the language is simple.
- mlevental 9y agothe link is a very good illustration of the value of monads if you understand monads. that's not a criticism - just yesterday i was complaining about their value and this has made me reconsider.
- jimbokun 9y agoIt is also the best way to explain what a monad is, if you are a programmer who does not understand monads.
- jimbokun 9y agoThe article you link to is by far the best introduction to Mondas I have ever seen. 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!
- ebiester 9y agoIt's a tradeoff. I may not get monads with Kotlin, but I also never have to understand (or explain) code like: 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.
- lmm 9y agoSo what would the equivalent Kotlin look like? 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.
- rbehrends 9y agoI see two problems with this line of argument. One is that specializations are useful and are often more prevalent in practical use. Examples: 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-ecoop98.pdf https://homes.cs.washington.edu/~mernst/pubs/dispatching-eco... [2] http://seaside.st/about/examples/counter http://seaside.st/about/examples/counter
- lmm 9y ago> 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. 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.
- venturis_voice 9y agoHi, thanks for commenting on our recent blog post. We share and discuss all our content on Venturi’s Voice Slack group. Seeing as you know quite a bit about this topic it would be great if you joined the conversation on there. We’re a new community based around the blogs and podcasts we release everyday at Venturi. We aim to consistently deliver valuable content to our candidates - but we know there is always room for improvement, so your feedback would really help us out! 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/invites/new https://publicslack.com/slacks/https-venturisvoice-slack-com...