Scala Case Class
When I was reading Scala language and see case class, I found it very useful but I didn’t understand it correctly and confused with…
Scala Case Class
When I was reading Scala language and see case class, I found it very useful but I didn’t understand it correctly and confused with Companion Object and companion class until I created a list of case class properties that I’ll write them here.

Case class and companion object
Companion Object: Case class create a class with companion object, Singleton object define with ‘object’ (When object name the same of its class and the same source file. In Java having a class with static methods). Access methods and fields that are private in class. Common use of companion object are: => Factory Method of class => Create extractor for the class
Apply/Unapply Method: Implement the apply(Factory Pattern) method you can use as a factory. Let’s you create instance without new keyword. ‘apply’ serves the purpose of closing the gap between Object Oriented and Functional Programming in Scala (Apply method is the way that connects Functional Programming to Object Oriented). -> Every Function can be represent as an object -> Every Function also has an Object Oriented type: f(Int) => Int will have Object Oriented type of Function[Int, Int] Every Function we call a function: f(x). We call f.apply(x) in Object Oriented way. Unapply: Since the compiler implements the unapply method, a case class support pattern matching. This is specially important when you define ADT (Algebra Data Type). Unapply method lets you deconstruct a case class to extract it’s field.
Prefix all arguments, in the parameter list with val. this mean class is immutable.
Always Compared Structurally: Adds natural implement actions of hashCode, equals (==) and toString.
Generate the copy method.
Inherit product trait -> product Arity: Size -> product Elements: retrun nth element -> Product Prefix: class name
Function with all arguments as parameters, like: Person.apply_ Person.curried (Curried Method)
used to mimic algebraic datatypes.
Case class cannot use as an implicit class. An implicit class is a type of class that provides an automatic conversion from another class. By providing an automatic conversion from instances of type A to type B, an instance of type A can appear to have fields and methods as if it were an instance of type B.
메타데이터
- post_id
- e363c7d4e04e
- slug
- scala-case-class-e363c7d4e04e
- url
- https://medium.com/@majidin/scala-case-class-e363c7d4e04e
- canonical_url
- https://medium.com/@majidin/scala-case-class-e363c7d4e04e
- author_url
- https://medium.com/@majidin
- status
- ok
- fetched_at
- 2026-07-29 20:22:19