5/21/2023 0 Comments Functional programming in scala![]() It explains the same topic and talks about typelevel validations. Additionally, if you like to get even more typesafe, consider reading PFP book. Using newtypes or value types also perfectly fits into the DDD approach as it has the similar concept. You should choose based on your project needs. All of them have their strength and weaknesses compared to newtypes. Other techniques in Scala exist that allow you to do the same things, for example, AnyVal, opaque type aliases (Scala 3), and tagged types. This requires a bit more ceremony of defining and building these values of new types, but in exchange, you get advanced type safety, and, as a bonus, your code base becomes easier to read because strong types form a self-documenting code. With a declaration that uses libraries like scala-newtype or monix/ newtypes, the compiler will generate an error if you attempt to mix up different types. Let’s see an example of using weakly-typed values in the case class that models audio settings. And while it is perfectly fine to use such simple types in the local scope, it might cause trouble when used across public methods and models. ![]() We will start with an anti-pattern highly discouraged in the functional programming community - weakly-typed values.īy “weakly-typed,” we usually mean values of types like String, Int, Long, Boolean, UUID, and so on, i.e., types that are not concerned with their real meaning for our program, be it ids, names, various settings, flags, etc. Using Weakly-Typed Values (Aka Primitive Obsession) Let’s start with common anti-patterns not specific to using cats-effect or any other FP library. We will also focus on misusing the cats-effect library and guide how to avoid common mistakes. ![]() This article will discuss common code smells and anti-patterns that can arise when using FP in Scala. However, it still can result in code smells and anti-patterns. FP is a powerful concept used to write code safely, in an easily testable and refactorable manner. In this article, I want to help you avoid common mistakes when using FP libraries in Scala and develop some intuition into properly using it. Let’s get to the main topic of our discussion. FP Anti-Patterns in ScalaĪll right, enough of ChatGPT. Or an article like this that maybe the next versions of AI assistant will index and pick up for its answers. An experienced mentor who has a lot more of the context. To be able to draw more concrete examples of best practices, you need more than AI can give you (yet). I do not recommend using ChatGPT for cases like that because it might be misleading and give you completely wrong intuition. I tried other prompts, but all the answers needed more insight and gave wrong code examples. As you can see, although it has a point, most of the statements here are generic at best, and some of them are just silly.
0 Comments
Leave a Reply. |