Idiomatic Error Handling in Scala | Rock the JVM

  Рет қаралды 9,255

Rock the JVM

Rock the JVM

Күн бұрын

Пікірлер: 34
@user-uf4lf2bp8t
@user-uf4lf2bp8t 4 ай бұрын
I mainly write rust, but scala, being arguably more open to different paradigms than rust, does functional programming so well. I really hope ergonomic HKT's will be added to rust one day.
@MarcusHammarberg
@MarcusHammarberg Жыл бұрын
Brilliantly explained. Loved the build up of pros and cons
@maciejszklarzewski5764
@maciejszklarzewski5764 3 жыл бұрын
Wow! I have seen Validated for the first time and I'm quite impressed with the power of functional programming :D Thanks for an awesome video!
@rockthejvm
@rockthejvm 3 жыл бұрын
Glad it helps!
@shakilhaque1822
@shakilhaque1822 3 жыл бұрын
Daniel, no offence at all but im gonno say it....this video is better than the 'handling failure' on your beginner course. Its all making sense now after watching this.
@rockthejvm
@rockthejvm 3 жыл бұрын
Glad it finally does - I'm redoing the beginner course for Scala 3.
@rush18757
@rush18757 3 жыл бұрын
Thank you Daniel, best explanation I found about Validated. You just forget to mention Fold method of Either that I find very useful.
@rockthejvm
@rockthejvm 3 жыл бұрын
Glad it helps - of course, the API is quite comprehensive.
@aethermass
@aethermass 3 жыл бұрын
Good video. I was initially a Java dev and moved to Scala a few years ago. I kept some bad habits such as using the first method here. I will be switching to cats for this and see what else they have. Your cats course sounds interesting.
@rockthejvm
@rockthejvm 3 жыл бұрын
Glad it's useful!
@estebanmarin002
@estebanmarin002 Жыл бұрын
Thanks for the explanation! I was thinking that the fact that does not have `flatMap` means that we can do `.mapN(` and parallelize all the validations.
@no_more_free_nicks
@no_more_free_nicks 3 жыл бұрын
24:19 - maybe it would make more sense to have a Semigroup (where this names comes from???) with identity?
@rockthejvm
@rockthejvm 3 жыл бұрын
Identity is a single-arg function. Here we need two arguments. Semigroup is a math term, where you have a set of items (for us, a type), and a combination function which turns two arguments from the set/type into another value from the set/type.
@no_more_free_nicks
@no_more_free_nicks 3 жыл бұрын
@@rockthejvm Thanks for explanation!
@basheeral-momani2032
@basheeral-momani2032 2 жыл бұрын
thats a hard one, thanks for the great explanation
@MercedeX7
@MercedeX7 3 жыл бұрын
Exception means something exceptional happened and program must do something about it. Often we report & exit the app. However i don't understand the logic for every try-catch as an expression. If we are supposed to read a file and file is not there we must exit, what's the point of using try-catch expression if we cannot continue ?
@rockthejvm
@rockthejvm 3 жыл бұрын
Exceptions are not the end of the world - even in Java you have try-catches to recover from error. Here in Scala, the entire thing returns a value.
@luismaini
@luismaini Жыл бұрын
great explanation, thanks
@Lumintorious
@Lumintorious 3 жыл бұрын
Aah I was really excited for the Validated data type (because Either feels more like a Sum type rather than a Ok/Err data structure and the fact that it's left/right not wrong/right gives me an aneurysm xD), but I'm disappointed it's not a monad since I love chaining conditions in for comprehensions. Do you think that if I were to learn Cats and include it in my projects I would have to code in the idiomatic "purely" functional Cats way or can I use what I need from there without turning it into a religion?
@rockthejvm
@rockthejvm 3 жыл бұрын
Not really - Cats is quite flexible.
@luismiguelmejiasuarez2020
@luismiguelmejiasuarez2020 3 жыл бұрын
You can see cats either as a tool-box of common functionalities (like Validated) or as building blocks for writing fully parametrized code (or the so-called tagless final style). The amazing thing is, much like Scala, is not an either one or the another but rather a spectrum, so you can move inside it freely as you please.
@luismiguelmejiasuarez2020
@luismiguelmejiasuarez2020 3 жыл бұрын
Great video, but I believe it would have been better to expand a bit more on the Validated part. First, it is common to use a non-empty collection for the invalid case, since if you have an invalid is because you have at least one error. And since you will be concatenating errors together then it makes sense to use something that it's optimized for that; enter NonEmptyChain - also, this is so common that we have type ValidatedNec[E, A] = Validated[NonEmptyChain[E], A] Second, Validated does provide a flatMap like method (since it is isomorphic with Either) it just uses a different name for convention, it is called andThen. Third, the two most common ways of using Validated are not using combine, rather using mapN or traverse - The first one, to combine multiple independent validations such that we apply a function if all values are valid or we combine all invalids; e.g (validateName(rawName, validateAge(rawAge)).mapN { case (name, age) => Person(name, age) } . The second one, is used to "reduce" a list of independent validations into a list of valid values, if all are valid, or collect all the errors; e.g. List(1, 2, 3).traverse(validateEven) Last but not least, it is now recommended to not use Validated directly but rather use the parallel instance of Either. That way, you can use only a single datatype and chose if compose the validations in a fail-fast or accumulating semantics on each use site. Basically by using flatMap for the former and combinators like parMapN or parTraverse for the later.
@rockthejvm
@rockthejvm 3 жыл бұрын
Thanks for the feedback - here are my thoughts: 1. For the Nec thing, my goal is to keep complexity to a minimum since I expect most watchers are not familiar with Validated at all. 2. andThen is not the same as flatMap, because it short-circuits error accumulation - the name is appropriate and the Applicative (not Monad) property stands 3. Again, for the sake of minimizing complexity for people not familiar with Validated. I'll probably add a dedicated video on validation and traversal. 4. I'll probably add more nuance in another more advanced/subtle video.
@luismiguelmejiasuarez2020
@luismiguelmejiasuarez2020 3 жыл бұрын
@@rockthejvm yeah I believe an in-depth video about Validated and Either + Parallel would be great! "andThen is not the same as flatMap, because it short-circuits error accumulation" Yeah, that is exactly the point, andThen behaves exactly as flatMap would behave. You can not accumulate errors on a flatMap call, because if you have v1.flatMap(f) and v1 failed you can not call f so it can't fail, also if f fails is because v1 succeded; as such there doesn't make sense to combine errors because there can never be two, only one. Is not called flatMap, because by convention flatMap has to be coherent with ap but this is not the case and to be coherent we would need to change ap, which would lose the whole point of Validated.
@AJKvideoproductions
@AJKvideoproductions 3 жыл бұрын
Awesome
@rockthejvm
@rockthejvm 3 жыл бұрын
:D
@MarcusHammarberg
@MarcusHammarberg Жыл бұрын
> Left side is undesirable, Right side is desirable WHY is this convention not more prominent in all places talking about Either. More often I read descriptions about this being unbiased and that either (hehe) side has equal values. Thanks a lot
@rockthejvm
@rockthejvm Жыл бұрын
Either was not originally intended for error handling, but ended up widely used for this purpose.
@MarcusHammarberg
@MarcusHammarberg Жыл бұрын
@@rockthejvm Ha! I figured that was the case, after seeing this video among other things. Thanks for your great content and this answer
@JimMillerDrums
@JimMillerDrums 3 жыл бұрын
Rocket science is easy. Rocket engineering on the other hand...
@rockthejvm
@rockthejvm 3 жыл бұрын
:D
@user-zv9um9pb6w
@user-zv9um9pb6w 3 жыл бұрын
Rocket surgery is very very hard
@no_more_free_nicks
@no_more_free_nicks 3 жыл бұрын
Where is the UFO operator? If I remember well: |@|
@rockthejvm
@rockthejvm 3 жыл бұрын
I'll probably talk about semigroups and monoids in another video.
What the Functor? | Functors in Scala | Rock the JVM
24:43
Rock the JVM
Рет қаралды 12 М.
UFC 310 : Рахмонов VS Мачадо Гэрри
05:00
Setanta Sports UFC
Рет қаралды 1,2 МЛН
Каха и дочка
00:28
К-Media
Рет қаралды 3,4 МЛН
How to Sort Lists with Tail Recursion in Scala | Rock the JVM
20:06
Zymposium - Error Handling in ZIO
1:04:43
Ziverge
Рет қаралды 1,1 М.
Tagless Final in Scala
36:13
Rock the JVM
Рет қаралды 11 М.
Contravariance in Scala: Why Is It So Hard? | Rock the JVM
16:24
Rock the JVM
Рет қаралды 15 М.
The Death of Monads? Direct Style Algebraic Effects
17:13
Impure Pics
Рет қаралды 20 М.
Scala: Beyond the basics with Christopher Batey
48:05
Devoxx
Рет қаралды 49 М.
Martin Odersky - Scala's Role in the Programming Languages Ecosystem
45:53
Scala Days Conferences
Рет қаралды 13 М.
Self-Types in Scala - the Why and the How | Rock the JVM
14:57
Rock the JVM
Рет қаралды 8 М.
Types, Kinds and Type Constructors in Scala | Rock the JVM
17:53
Rock the JVM
Рет қаралды 7 М.
UFC 310 : Рахмонов VS Мачадо Гэрри
05:00
Setanta Sports UFC
Рет қаралды 1,2 МЛН