vbsteven
04/02/2023, 3:33 PMResult<T>
to make the the error explicit in the signature, or I can add a @Throws
annotation and just throw the exception. Currently I'm favoring the Result<T>
approach but it has some issues (like the Exception type not being generic).
Any opinions on this?hfhbd
04/02/2023, 3:40 PMvbsteven
04/03/2023, 5:41 AM@Throws
is that a lot of these methods can frequently "throw" and aside from the annotation on the signature there is no other indication that it can. AFAIK Intellij does not warn when calling a @Throws
method that does not handle the exception. Given the tendency for many developers to not read the docs I'm afraid many of these will be missed leading to frequent runtime crashes (not ideal in a GUI library).
I put "throw" between quotes because the methods I'm talking about are generated bindings from C code, which use an error pointer argument that gets populated when the C function returned early with an error.vbsteven
04/03/2023, 5:45 AMvbsteven
04/03/2023, 5:49 AMResult<T>
. I put getOrThrow()
in there for demonstration purposes. If the openReadWrite
uses @Throws
, then it is easy to forget to handle the case where the file does not exist, the file is a directory, or the operation was cancelled (if a cancellable was provided and later used asynchronously)vbsteven
04/03/2023, 5:55 AMvbsteven
04/03/2023, 6:00 AMResult<T>
. The benefit is that the user will be forced to do something with the failure case. My main issue with this solution is that ex
from Result<T>
is a Throwable
while I already know it cannot be anything else than GlibException
or one of its subclasses.vbsteven
04/03/2023, 6:05 AMResult<T>
which narrows down the exception type, but it feels dirty.hfhbd
04/03/2023, 7:43 AMResult<T>
isn't a common practice.
The mapped Java File APIs are similar:
File("foo.txt").writeText("foo")
This could also fail with IOException, but there is no hint/language design to explicitly catch the exception.
But Kotlin's design is also pragmatic. If it does work for you, use your API design, in this case, I would use your own Result
class to provide more information.vbsteven
04/03/2023, 8:01 AMSoren Roth
04/04/2023, 6:03 AM...OrNull
variety of functions which catch all exceptions and return null.vbsteven
04/04/2023, 8:06 AMResult<T>
approach looks interesting (which is the current implementation) is that it provides some flexibility to the library user. The Result<T>
class provides some utilities like getOrNull()
, getOrThrows()
.vbsteven
04/04/2023, 8:08 AMResult
type that encapsulates the GError details more clearly using the type system.