A lazy future

This blog is a follow up from the previous one and builds up on our small effect experiment. As good as our abstraction goes it still wraps a plain Scala Future which is eager. Meaning that as soon as you create a Future it starts executing on the provided ExecutionContext.

Our effect wrapper F suffers the exact same issue as it just wraps a Future.

final case class F[+E, +A](value: Future[Either[E, A]]) extends AnyVal

Why is that a problem? Simply because it makes it harder to reason about the code.

Continue reading “A lazy future”

The problem with EitherT

If you’ve used Scala Future in a real application you probably realised that having a failed Future wrapping any Throwable isn’t the most efficient way to handle errors in your application.

I used Scala Future in this example but it applies equally to cats effects. Any type like IO[A] suffers the same issue in that regard as it doesn’t provide an error channel other than Throwable.

It’s basically the same issue as the undocumented Java RuntimeException. The only way to know what exceptions are thrown is to look at the implementation (when it’s possible).

Continue reading “The problem with EitherT”

Dealing with errors

As a programmer we don’t really like to deal with errors. We like to focus on the happy path – the one that provides value – and deal with the errors later because … well, we have to do it!

However dealing with failures is crucial if we don’t want our program to stop on the first error it encounters.

But still we don’t want to mix the “clean” code of the happy path with the “dirty” error handling code. And in fact this is what exceptions were suppose to bring: A clean happy path in a try statement and the error handling code in a catch statement. You know everything clean and separated.

In Scala we have a rich type-system which gives us more options to handle errors. But before we start let’s be clear by what we mean by errors. Continue reading “Dealing with errors”

Leveraging the type system to avoid mistakes

We all know we should write tests to make sure our system behaves as it is supposed to.

Surely tests are necessary to ensure correctness of our programs but they only depend on what the programmer is willing to test (or can think of testing).

What I mean is that there will always be gaps in the test coverage, like uncovered corner cases or improbable combinations of events, …

In Scala we have a powerful type system that we can use to help us avoid some mistakes.

Why would you bother writing a test to make sure a function handle some corner cases correctly when you can use the type system to make sure such cases won’t ever happen. Continue reading “Leveraging the type system to avoid mistakes”

Refined types, what are they good for?

Type refinement is all about making the types more precise. But why would do that? Because using the correct types makes your program safer as you reduce the possibility to introduce bugs.

First let’s think about types. For instance String is a type we use all the time. A variable of type String can have many different values (in theory an infinity of values) but it’s quite unlikely that all these values make sense in our application. Continue reading “Refined types, what are they good for?”

Akka stream interface for gRPC

Back from holidays let’s continue with some of my favourite topics: AkkaStreams and gRPC.

We’ve already seen how can take advantage of the ScalaPB code generation tool to generate new interfaces (GRPCMonix) on top of the grpc-java implementation or to create new tools to integrate gRPC with other services (GRPCGateway).

Similarly to GRPCMonix which provides a Monix interface – Task, Observable – on top of gRPC, it’s possible to develop an AkkaStream interface on top of gRPC. Continue reading “Akka stream interface for gRPC”

Introduction to Tagless final

In this previous post we’ve seen that before using Scala’s Future it might be worth taking some time to think of the use cases (especially error cases), the execution model we need, … as it might be more advantageous to choose a solution like Monix’s Task (although not available in standard library) to gain finer control over the execution. However some might not be able to make a decision at this stage and like to keep their options open. Let’s see how we can revisit our product repository in such way that we don’t have to make a decision too early.
Continue reading “Introduction to Tagless final”