News for hackers. Stuff that matters.
Re:Typesafe Error Handling in Kotlin by mamcx on Tuesday December 17, 2019 @09:57AM (#21814016)
One of the most use-full things of Rust is to have a clear "conversion" trait:

https://doc.rust-lang.org/std/convert/trait.From.html

This is my #1 thing with rust.

Is a stroke of genius to have a clear API for make conversions.

Re:Typesafe Error Handling in Kotlin by vmchale on Tuesday December 17, 2019 @11:08AM (#21814903)
Is there any writeup of the theory behind this? I'm particularly interested in type inference.
Re:Typesafe Error Handling in Kotlin by killercup on Tuesday December 17, 2019 @03:32PM (#21818460)
The RFC for adding `?` to the language might be a good start: https://rust-lang.github.io/rfcs/0243-trait-based-exception-...
Re:Typesafe Error Handling in Kotlin by joostdevries on Tuesday December 17, 2019 @06:43AM (#21812610)
The monadic approach to exceptions like it's done here has the downside of having to encode all your business logic in maps and flatmaps. All the intermediary functions will have to return Try. And all the Try values have to combined into one. That type fidding code feels like cruft that doesn't add much value.

And I say that having developed in Scala for years and years.

Of course there's still sometimes when you want processing of other items to proceed, even though this one has failed. And that's where Try is really essential. So that's where we use it only: at the topmost level.

It's only at that top level that we wrap the call in a Try. And that works rather well.

One reason to do it like this is that we want to leverage the design philosophy of Kotlin. And not be writing Scala or Haskell in Kotlin ). And Kotlins philosophy when it comes to exceptions turns out to be: exceptions. Which is apparent in Kotlin coroutines, channels and flows.

) that's also why we try to stay away from Arrow

Re:Typesafe Error Handling in Kotlin by ragnese on Tuesday December 17, 2019 @08:31AM (#21813232)
Definitely fair points. I do feel like I'm swimming upstream even when I'm using the built in Result class in Kotlin.

Truth is, I rather be writing Rust, but I need the JVM sometimes. ;)

Re:Typesafe Error Handling in Kotlin by vmchale on Tuesday December 17, 2019 @11:10AM (#21814929)
> All the intermediary functions will have to return Try.

That's the advantage of the monadic/functorial approach: they don't!

Re:Typesafe Error Handling in Kotlin by benol on Tuesday December 17, 2019 @04:52AM (#21812136)
It's worth pointing out that Kotlin's standard library contains a type called `Result` with a slightly different API. Also a function called `runCatching`[1] that uses it.

[1] https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/run-catc...

Re:Typesafe Error Handling in Kotlin by manojlds on Tuesday December 17, 2019 @04:57AM (#21812164)
What are these .Christmas TLDs? Seeing those crop up and only makes sense at this time of the year right?
Re:Typesafe Error Handling in Kotlin by outadoc on Tuesday December 17, 2019 @05:12AM (#21812230)
Re:Typesafe Error Handling in Kotlin by BiteCode_dev on Tuesday December 17, 2019 @06:39AM (#21812591)
I kinda like it, although here it's clearly an marketing attempt and probably also an SEO move of some sort.

Still, it's "python xmas!" all year long would be a nice theme for a blog :)

Re:Typesafe Error Handling in Kotlin by RivieraKid on Tuesday December 17, 2019 @04:09AM (#21811933)
Honestly, I prefer to have the option of using checked exceptions, it's often leads to better readability than using Result. This is my main issue with Kotlin.
Re:Typesafe Error Handling in Kotlin by ragnese on Tuesday December 17, 2019 @08:26AM (#21813181)
I feel like I'm the only crazy person who thinks checked exceptions are better than unchecked exceptions. I've read all the arguments against them and they just don't resonate with me. "Oh. So you think it's okay that a dependency's method changed to throw a new exception, but you don't want to have to change your code? Sounds super lazy and risky to me..." When your dependency's logic changes, yours probably needs to, as well.

What really drives me nuts is when the same person shits on checked exceptions and then advocates for using a monadic return value. They're completely isomorphic. The only difference is in how it reads.

Re:Typesafe Error Handling in Kotlin by lenkite on Tuesday December 17, 2019 @10:20AM (#21814309)
Java is the only language that has checked exceptions AFAIK.

Checked exceptions just lead to interface pollution. You really don't know what type of exception can be thrown by an implementation of a facade when you are defining the facade. And so then you are forced to define a InterfaceOperationException and then force all callers to wrap their exceptions in this type. Even if you use an implementation that is exception-free, you are forced to catch.

Painful and elaborate ceremony for very little gain. Usually exception handling is centralised at a few places in the code. Catching exceptions at these areas and boundaries is the best place instead of littering handler code all over the code base.

Oracle has realised that checked exceptions are a mistake. None of the java functional interfaces declare checked exceptions. If your operation throws a checked exception, it cannot be used as a convenient method reference lambda. It does not decay into a standard functional interface. This alone is an ultra critical-strike against checked exceptions.

If a condition is so important that you need to declare it as a checked exception, it generally indicates it should have been be a return type or your functional granularity is large. And normal types, unlike exception types, are well-suited to generics.

Re:Typesafe Error Handling in Kotlin by RivieraKid on Tuesday December 17, 2019 @03:12PM (#21818212)
Swift does have checked exceptions too (at least by my understanding of the term "checked exception"). In general I think Swift is more thoughtfully and elegantly designed than Kotlin, people usually say that they're very similar but I find Swift more pleasant read and write.
Re:Typesafe Error Handling in Kotlin by ragnese on Tuesday December 17, 2019 @04:04PM (#21818815)
Yes and no. In Swift you have to mark if a function throws, but not WHAT the function throws.

The Java-style checked exception haters might see that as the best of all worlds.

I generally agree that Swift is a little nicer than Kotlin. I like the enums better than sealed classes, I like that Swift has value types.

The only think from Kotlin that I miss in Swift is the scoping methods: apply, with, let, also.

Re:Typesafe Error Handling in Kotlin by lmm on Tuesday December 17, 2019 @08:58AM (#21813437)
The difference is that monadic result types work properly with generics. You can't really use checked exceptions generically at all (simplest example: you can't pass a function that throws a checked exception as a callback function, unless you write multiple overloads of any function that takes a callback), which really limits your ability to reuse code.
Re:Typesafe Error Handling in Kotlin by saghm on Tuesday December 17, 2019 @01:25PM (#21816773)
> What really drives me nuts is when the same person shits on checked exceptions and then advocates for using a monadic return value. They're completely isomorphic. The only difference is in how it reads.

Is that true? I was under the impression that annotating a certain type of exception being thrown doesn't prevent other (i.e. unchecked) exception types from being thrown from a method. If that's true, then that's a pretty significant different between monadic returns and checked exceptions; in the former case, returning an unknown error type leads to a compiler error, but in the latter, it leads to a crash at runtime.

Re:Typesafe Error Handling in Kotlin by RivieraKid on Tuesday December 17, 2019 @03:04PM (#21818127)
Unchecked exception can be thrown and cause a crash in both cases.
Re:Typesafe Error Handling in Kotlin by joostdevries on Wednesday December 18, 2019 @08:21AM (#21824494)
When designing C# Anders Heljsberg decided against following Java's checked exceptions. You can find his argument against in the interview from 2003 https://www.artima.com/intv/handcuffs.html
Re:Typesafe Error Handling in Kotlin by cies on Tuesday December 17, 2019 @05:02AM (#21812182)
Really? Exceptions introduce some sort of magic in control flow, it is simply less explicit. It may come in handy, but I rather use it as a final resort than as a way to return different types (I've seen exceptions being used in Java for non-error cases I guess because sum-types (aka tagged unions) are not available).

Having this (together with pattern matching) as the-way-to-do-errors in Kotlin is one of the main reasons I feel it fixes what Java cannot (easily) fix.

Can you articulate how it increases readability?

Re:Typesafe Error Handling in Kotlin by Razengan on Tuesday December 17, 2019 @06:05AM (#21812467)
Do you mean like Swift? (which also has a Result type)

https://docs.swift.org/swift-book/LanguageGuide/ErrorHandlin...

Re:Typesafe Error Handling in Kotlin by RivieraKid on Tuesday December 17, 2019 @03:12PM (#21818225)
Yes. Swift and Julia re my favourite languages.
Re:Typesafe Error Handling in Kotlin by ragnese on Tuesday December 17, 2019 @08:35AM (#21813258)
I'm a little confused by something. I wouldn't have expected his/her example code to compile:

  fun divide(dividend: Int, divisor: Int): Try<Int, String> =
      when (divisor) {
          0 -> Try.Err("Cannot divide by zero!")
          else -> Try.Ok(dividend / divisor)
      }
The first branch of `when` is of type `Try<Nothing, String>`, the second branch is `Try<Int, Nothing>`, and the return value is supposed to be `Try<Int, String>`. None of those line up...

How is that possible? Does the compiler do some magic for the `Nothing` type? The definition looks pretty damn simple:

  public class Nothing private constructor()
Re:Typesafe Error Handling in Kotlin by greiskul on Tuesday December 17, 2019 @08:50AM (#21813383)
It's not possible in Kotlin to have an instance of Nothing, and the compiler does some magic for the Nothing type, in that it can be converted to any other type in the system (so it's like the opposite of the Any type). This allow code patterns like:

''' val personName = person.name ?: throw IllegalArgumentException("Name required") '''

Where the type of personName is now String, cause if name was null it would throw an exception (and the type of a throw expression is Nothing).

Re:Typesafe Error Handling in Kotlin by lmm on Tuesday December 17, 2019 @08:55AM (#21813413)
Nothing is (magically) a subtype of every type, and Try is covariant in both its parameters (Kotlin has its own non-standard terminology for variance that I can't remember, but what it really means is covariant).
Re:Typesafe Error Handling in Kotlin by UserIsUnused on Tuesday December 17, 2019 @05:00AM (#21812171)
Why not just use sealed classes for your given use-case rather than a generic result? If you want to do generic why not go full Either?
Re:Typesafe Error Handling in Kotlin by mrkeen on Tuesday December 17, 2019 @06:01AM (#21812452)
Maybe the people that realise that are the people who self-select into a language where Result and Either are just implementation details and should be hidden behind F[_] or similar.
Re:Typesafe Error Handling in Kotlin by jbverschoor on Tuesday December 17, 2019 @04:23AM (#21812001)
Ah right another thing copied from swift.
Re:Typesafe Error Handling in Kotlin by Sharlin on Tuesday December 17, 2019 @05:09AM (#21812215)
The fact that you were introduced to concept X by language Y rarely means that X was invented by Y.
Re:Typesafe Error Handling in Kotlin by cies on Tuesday December 17, 2019 @04:30AM (#21812042)
I think a lot of languages implemented this before Swift did. Some MLs (among them OCaml), Haskell, Reason, Elm, Rust.

So crediting Swift for this is not fair.

Re:Typesafe Error Handling in Kotlin by UK-Al05 on Tuesday December 17, 2019 @08:09AM (#21813085)
It's actually a fairly old method, that's recently gained popularity as functional programming interest increases.
Re:Typesafe Error Handling in Kotlin by vmchale on Tuesday December 17, 2019 @11:11AM (#21814953)
I think monadic do comes from Haskell and its ancestors.
Re:Typesafe Error Handling in Kotlin by exdsq on Tuesday December 17, 2019 @12:28PM (#21815963)
I’ve finally reached the age where I was using something before a language that ‘copied’ it even existed!

This was around in Haskell before Swift existed.

Re:Typesafe Error Handling in Kotlin by vmchale on Tuesday December 17, 2019 @11:12AM (#21814957)
I'm pretty sure monadic error handling came from some ML