What's the issue with the Scala Stdlib?

Many answers to questions here result in the suggestion to use cats or other external libraries.

As a newbie, this makes me ask myself: What is wrong with the Scala Standard Library? Why is everyone so keen to use some external library?

I am asking this question because my instinct as a developer is that I want to reduce the number of external dependencies to a minimum to keep track of updates, bug fixes, security issues, and so on.

To be very blunt: Is there something wrong with the Stdlb?

1 Like

The standard library’s scope is limited on purpose. Its creators don’t see this as a problem, but you could. Certainly in the past there have been calls for adding this or that to the standard library. If it doesn’t provide functionality you need and don’t want to implement yourself, you’ll be pushed to add a dependency. Often the authors of a library have spent a lot of time making sure it works well, and they are as trustworthy as the official Scala developers.

1 Like

There is nothing wrong with it. But it’s not comprehensive (and not intended to be), and probably some things are still the way they are just for historical reasons - it’s harder to change the API to the std lib than some 3rd party lib. And it’s compromising between the “better Java/OO” angle and the “functional on the JVM” one (and doing a pretty decent job at this, IMHO).

Thus it certainly doesn’t hurt to know what alternatives to std lib features are provided by 3rd party libraries - in particular if they’re explicitly opinionated towards a specific paradigm like cats/scalaz. The choice will always be yours.

4 Likes

Don’t get me wrong: I don’t question the quality of the 3rd party libraries or the skills of the developers. And I understand why we would use 3rd party libraries.

But if I look, for example, at modern JavaScript UI stuff I see so many different 3rd party libs used. How is this in the Scala ecosystem? Do people decide for one of those large 3rd party libraries and use its features (like in Java Apache Commons vs. Guava) or do we lean towards a state where we will depend on multiple external libraries?

This is not necessarily an exclusive choice. :wink:

PHP has a kitchen sink standard library with plenty of deprecated functions and there are many articles about which functions from PHP standard library we shouldn’t use. There’s a cost of throwing everything “seemingly useful” into standard library.

OTOH stdlib of Scala 2.13 has plenty of new functionality and is often sufficient.

There’s no “right” choice about when to include something in stdlib. It’s rather arbitrary - subject to language authors’ preferences. OTOH a big factor is the manpower - if there are too few people with enough time to do proper review and ongoing maintenance then there’s a resistance.

3 Likes

I have used Scala for a really long time and never used Cats or Scalaz etc. I actually discourage using niche libraries as it makes the language harder for people to learn and code harder for others to read. If I was part of a project where everyone was really into Scala and Cats/Scalaz, I would use it but that is usually not the case.

4 Likes

That’s what I also think, why I try to stick to the StdLib as much as possible.

Ok so, I am guilty of this. So, let me try to fix my mistake.

It was never my intention to discourage the stdlib, nor to give the impression that it is useless.
I actually find it very useful and complete.

Curiously, contrary to what had happened latter here, usually in the gitter channel and in SO I try to answer with stdlib only solutions.

However it is true that I find cats to be very useful and I consider it as an extension to the stdlib, or a core library. Since, for me, it is really a toolbox of useful functions and abstractions that can make your life easier (especially the more FP you try to be).

I disagree with @mohitjaggi about cats being difficult to understand or harder to use (and certainly I wouldn’t call it “niche”).
In my experience reading scala with cats and doing the exercises is enough to understand the basis of the library and start using it, and in my experience that can be done in two or three weeks using only a couple of hours of free time.

It is true that the library is actually very big and that the book just touches the “tip of the iceberg”, but as with most things, you will slowly learn more and more about it by using it. And, in my very personal experience, I have found myself using just what the book showed in 90% of the time.

Also for what I know, the library is actually commonly used in non-pure FP projects, like in conjunction with Akka or even Spark, as I said before, it is just a toolbox, which can be very useful.
(note this is just about cats-core, cats-effec and other projects of the typelevel ecosystem are another beast and I personally agree that not everyone should be using an IO monad for everything)

Finally, I would also like to point out that still using just the stdlib is very plausible.
I have a couple of scripts and small programs that do not really in any library, but I do add it to most of my projects once I know that it will be somewhat big and complex.


Bonus: I am a maintainer of neotypes one of the core principles of the project was to be “Lightweight” and “non-opinionated”, as such it doesn’t depend on cats in its core module, but it provides an integration module. While I o not regret the choice of being non-opinionated, I have found myself re-implementing a couple of useful functions that cats provide, being the famous traverse the first one.

1 Like

I hope you won’t take my original mail as a complaint against you. I strongly appreciate your help and always did :slight_smile:

I really was only wondering because even on StackOverflow many people hint to use cats or Scalaz.

Thank you again for your help here!

1 Like

I didn’t say it is difficult to understand. But as you pointed out it takes time. You mention reading a book that only scratches the surface and 2-3 weeks of 2 hours a day. Sometimes that effort is useful and sometimes not. It depends on the project you are working on.

The key thing is, the Scala stdlib is pretty unopinionated – it is a bunch of useful classes, that you can use to program in whatever style you prefer. (Mostly: there are some common Scala idioms, but by and large the stdlib tries to let you work anywhere on the OO-to-FP spectrum.)

By contrast, cats and scalaz are highly-opinionated libraries: they represent a particular approach to working in Scala. It’s a nice approach, and one I’ve been increasingly moving towards myself, and these libraries provide you with tons of tools for working in that way. But it’s very much a matter of taste, so it’s probably appropriate that they aren’t part of the standard toolkit. (And frankly, it’s much easier for them to evolve quickly as the field evolves if they aren’t part of the core.)

So you get a bunch of people suggesting them because those people like that approach, and are prepared to say that it’s a better way to program.

(Plus, cats has a bunch of little tools that don’t require you to go down the FP road, but happen to be immensely useful if you do, and happen to be pretty useful even if you don’t – the lines of what belongs in stdlib and what belongs externally are fuzzy. But as others have pointed out above, it’s way too easy to wind up with a gigantic mess of a stdlib, and Scala has intentionally tried to keep it more focused than that.)

5 Likes

I really want to lean towards the FP approach, and I began to look into cats, but I was scared away by the massive amount of things there are and not knowing how to start, and most importantly, where to start.

Any hints for me as a beginner?

Personally, I found that the best approach was just going through the Cats documentation, one step at a time, and really spending the time to understand what each section was saying. The Cats docs aren’t by any means complete, but I think they’re a great introduction, and give you enough info to get started.

The other part of that, though, is to give yourself time. I’m a couple of years into learning FP, and I’m by no means done yet. It’s a very different approach to programming, and it takes time to change mental habits.

On the plus side, it’s not a strict either/or. One under-appreciated thing about FP is that it is largely about compositionality: being able to think of every level, from a single function up to an entire huge application, in much the same ways, and build the bigger pieces from the smaller ones in sensible ways. So you can often start small, making bits purer, without having to commit whole-hog to changing all of your code at once. I’ve found that a good, practical way to ease myself into it.

2 Likes

There’s a track for cats on https://www.scala-exercises.org/ with good explanations and interactive exercises.

Also, herding cats from Eugene Yokota has been a great resource for me.

2 Likes

I’d also recommend the free book Scala with Cats. I find it well written and structured.

2 Likes

Not sure if it counts as advice or not, but I will tell you what worked for me; I will try not to make the story too long, .

I tried to learn about FP a couple of times while at university.
All those times we used Haskell and we read the Learn you a Haskell for great good book, which is great but it never made the click for me.

Latter, by unrelated circumstances I end up learning about Spark and thus Scala.
Spark was OK, but for whatever reason, I end up loving Scala; so I decided to take the Scala Specialization in Coursera.

Basically just at the start of the course, Martin explained that Scala was intended to be a mix between OOP and FP and I remembered my Haskell days which made me more motivated to learn more about Scala and FP (mostly because contrary to Haskell, Scala syntax was pleasant for me).

After the two first courses, I started to make all my university projects in Scala.
At that point of my life, FP for me meant using higher-order functions, being immutable, preferring Option over null and Either over throwing exceptions, and trying to keep your side effects out of your logic.

Time goes on I got my first job and got the opportunity to keep using Scala for a research demo.
During this development, I constantly cross upon the term Scalaz, which was a library for functional programming in Scala (a classical example was how to combine a list of maps by summing their values for repeated keys); however, I always decided not to use the library because in my head it was wrong to pull out a really big dependency just for a single function.

Then one day I decided to watch the Scaladays talks and I found one whose title was pretty interesting: Functional Programming with Effects. I knew the word effect from my Haskell days, but I realy never understood what it meant and what was the point of wrapping a side-effect over some IO Monad? (what does that even mean).
So, I decided to watch the talk. It turns out it was one of the best talks I have even seen, the author made everything very clear and easy to understand.

After that talk I got more interested in those Functors, Monads, Monoids, Hemorrhoids and whatsoever, so I decided to read the Scala with Cats book and start using the library.

TL;DR;

  • The talk and the book were the ones that made the click for me.
  • Learning FP as @jducoeur said, is a process, you will be learning more and more by doing.
  • Getting in touch with the community helps a lot. Apart from the site, I recommend the Gitter room, I was there for like a year, completely silent, just reading what other people said, I learned a lot.
1 Like

Thank you so much for your extended explanations!

I started to read Scala with Cats just today, and I believe I know the Scala Days talk you are talking about. I watched it once but I did not understand too much, because the mathematical concepts behind this stuff are not familiar to me. But maybe I should give it another try!

1 Like

It may or may not be helpful, but I absolutely adore the book How to Bake Pi – it was essential for me getting past the math.

The “math” behind both scalaz and cats is Category Theory, a branch of abstract mathematics that can come across as terribly forbidding if you come at it the wrong way. That’s what the book is great at: it is a short, approachable, frankly fun introduction to the topic. Every chapter starts with a recipe, and it then explains the mathematical principle exemplified by that recipe. I found that it’s hard to be intimidated by the math after that.

Mind, it won’t teach you a thing about programming: this is an abstract math textbook, written by one of the best teachers in that field. But it provides a lot of the motivations behind these libraries: the way of looking at the world that the implement. Personally, I found Cats a bit more approachable once I’d gone through that…

2 Likes

Unfortunately that book is sold out, let’s see if I can get it somewhere, tho.