Build Type Class in Scala 3
Learn how to build type classes in Scala 3, enabling generic behaviors and polymorphic operations across different types.
Join the DZone community and get the full member experience.
Join For FreeIn this blog, we will learn how to build a type class in Scala 3. I have already written about what are types of classes in Scala. If you want to learn in detail, follow this blog post Starting with Type Classes in Scala, and if you want to learn about Contextual Abstraction in Scala 3 – Given and Using. Now let’s dive into the process of creating a type class in Scala 3 step by step.
1. Define the Type Class Trait
Start by defining a trait that represents the behavior you want to abstract as a type class. This trait will typically declare one or more methods that define the desired functionality.
For example, let’s create a type class called Show
that represents the behavior of converting a value to a String
:
trait Show[A] { def show(a: A): String }
2. Provide Type Class Instances
Next, you need to provide instances of the type class for specific types. These instances define the implementation of the methods declared in the type class trait.
Instances are created using the given
keyword, followed by the type for which the instance is defined, and the with
keyword to specify the implementation.
For example, let’s create instances of Show
for Int
and String
:
given Show[Int] with { def show(a: Int): String = s"The number is $a" }
given Show[String] with { def show(a: String): String = s"The string is '$a'" }
3. Define Methods Using Type Class
Now, you can define methods or functions that make use of the type class. These methods will take parameters of the type class type and utilize the behavior defined by the type class.
To indicate that a method requires an implicit instance of the type class, you can use the using
keyword followed by the name of the implicit parameter.
For example, let’s define a method called print
that takes a value of any type A
and requires an implicit Show[A]
instance:
def print[A](a: A)(using showInstance: Show[A]): Unit = { val str = showInstance.show(a) println(str) }
4. Use the Type Class
Finally, you can use the type class by calling the methods or functions that depend on it. The compiler will automatically search for the appropriate instance of the type class based on the types involved.
In the example, we can use the print
method to print values of different types:
val number: Int = 11 val message: String = "Hello, Scala!"
print(number) // Output: The number is 11 print(message) // Output: The string is 'Hello, Scala!'
By following these steps, you have successfully created and utilized a type class in Scala 3. The type class abstraction allows you to define common behaviors that can be extended to different types, promoting code reuse and modularity and enabling polymorphic operations.
Published at DZone with permission of Prabhat Kashyap, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments