Value Classes in Scala | Rock the JVM

  Рет қаралды 5,102

Rock the JVM

Rock the JVM

Күн бұрын

Written version: blog.rockthejv...
Content referred to in this video:
Opaque types in Scala 3: • Scala 3: Opaque Types ...
Cats: rockthejvm.com...
Check out newtypes: github.com/est...
This is a video for more experienced Scala programmers. We'll solve a type of bugs which are easy to miss and hard to trace. We'll discuss the need for value classes, the pros and cons with respect to performance, then we'll look at a library which solves the problem with minimal overhead in Scala 2. Finally, we'll see how Scala 3 comes up with a language-level feature for this problem, in the form of opaque type aliases.
Follow Rock the JVM on:
LinkedIn: / rockthejvm
Twitter: / rockthejvm
Blog: rockthejvm.com...
-------------------------------------------------------------------------
Home: rockthejvm.com
-------------------------------------------------------------------------

Пікірлер: 19
@luismiguelmejiasuarez2020
@luismiguelmejiasuarez2020 3 жыл бұрын
Great video as always! - Just one minor note, using a case class for a newtype is a bad idea because of copy. So you can have BarCode(1-12345-12345).map(_.copy(code = "Whatever")) and now you have a faulty BarCode.
@rockthejvm
@rockthejvm 3 жыл бұрын
Thanks!
@aethermass
@aethermass 3 жыл бұрын
Thanks for taking us all the way to opaque types. I have been using value classes for a while without noticing issues but opaque types looks to be an improvement. Hopefully, having dependencies of Spark and Akka won't delay me from moving to Scala 3 for too long. I might separate out the Spark module if they take a long time.
@rockthejvm
@rockthejvm 3 жыл бұрын
Nice, glad to hear you found value in these!
@MirrorsEdgeGamer01
@MirrorsEdgeGamer01 3 жыл бұрын
@@rockthejvm Excuse the pun. Great video thank you.
@jimaaman
@jimaaman 3 жыл бұрын
Interesting! Thanks for the alternative to supertagged. The `@newtype` annotation seems like a much cleaner way to define the types.
@rockthejvm
@rockthejvm 3 жыл бұрын
Yep - glad it helped!
@woutermenninga8524
@woutermenninga8524 Жыл бұрын
Question about 20:50: this code actually does compile for me: I can assign any string to "val barcode: BarCode =". Was this changed for the release version of Scala 3?
@hafizullahqasemi7332
@hafizullahqasemi7332 2 жыл бұрын
great content as always !
@HoD999x
@HoD999x 2 жыл бұрын
question about 17:56 - you're createing an either every time. why don't you count this as runtime overhead?
@rockthejvm
@rockthejvm 2 жыл бұрын
You have to create some sort of error case, which would have happened anyway. It's the wrapper class that eliminates the runtime overhead.
@patriciogorin1375
@patriciogorin1375 3 жыл бұрын
Great video again, thank you! I have a question. I've been using refined types to accomplish this type of strong modeling. Is there a relationship between value classes or new opaque types? Again, thank you!
@rockthejvm
@rockthejvm 3 жыл бұрын
Glad you liked it! Value classes, newtypes and opaque types are different approaches to the same problem of "hiding" a piece of information (like a string) behind a completely new type that MEANS something in particular for you.
@robert33232
@robert33232 Жыл бұрын
Thanks you for the video! :-))
@frankhofmann9089
@frankhofmann9089 3 жыл бұрын
What editor/IDE are you using?
@rockthejvm
@rockthejvm 3 жыл бұрын
Intellij + Scala plugin: jetbrains.com/idea (not affiliated in any way, I just like the tool)
@JulienBertozzi
@JulienBertozzi 3 жыл бұрын
Thank you for your videos!
@Sleight94
@Sleight94 3 жыл бұрын
Luckily the macro plugin is not needed anymore in 2.13
@rockthejvm
@rockthejvm 3 жыл бұрын
Indeed.
How Scala 3 Givens Work with Scala 2 Implicits
11:40
Rock the JVM
Рет қаралды 2,3 М.
Semigroups and Monoids in Scala
23:05
Rock the JVM
Рет қаралды 6 М.
Chain Game Strong ⛓️
00:21
Anwar Jibawi
Рет қаралды 41 МЛН
IL'HAN - Qalqam | Official Music Video
03:17
Ilhan Ihsanov
Рет қаралды 700 М.
Self-Types in Scala - the Why and the How | Rock the JVM
14:57
Rock the JVM
Рет қаралды 8 М.
Why Should You Care About Referential Transparency?
19:56
Rock the JVM
Рет қаралды 4,5 М.
Types, Kinds and Type Constructors in Scala | Rock the JVM
17:53
Rock the JVM
Рет қаралды 7 М.
Let's Talk About Scala 3 Indentation | Rock the JVM
22:14
Rock the JVM
Рет қаралды 4,7 М.
Functions vs Classes: When to Use Which and Why?
10:49
ArjanCodes
Рет қаралды 172 М.
What the Functor? | Functors in Scala | Rock the JVM
24:43
Rock the JVM
Рет қаралды 12 М.
Scala 3: Opaque Types | Rock the JVM
18:15
Rock the JVM
Рет қаралды 6 М.
Contravariance in Scala: Why Is It So Hard? | Rock the JVM
16:24
Rock the JVM
Рет қаралды 15 М.
Idiomatic Error Handling in Scala | Rock the JVM
26:45
Rock the JVM
Рет қаралды 9 М.
This is the Only Right Way to Write React clean-code - SOLID
18:23
Chain Game Strong ⛓️
00:21
Anwar Jibawi
Рет қаралды 41 МЛН