Given:
case class FirstCC { def name: String = ... // something that will give "FirstCC" } case class SecondCC extends FirstCC val one = FirstCC() val two = SecondCC()
How can I get "FirstCC"
from one.name
and "SecondCC"
from two.name
?
An unapply method is generated, which lets you use case classes in more ways in match expressions. A copy method is generated in the class.
You can use: Class c = Class. forName("com.
The getClass() method is utilized to return the class of the given number. Method Definition: (Number).getClass. Return Type: It returns the class of the given number. Example #1: // Scala program of Float getClass()
A Scala Case Class is like a regular class, except it is good for modeling immutable data. It also serves useful in pattern matching, such a class has a default apply() method which handles object construction. A scala case class also has all vals, which means they are immutable.
def name = this.getClass.getName
Or if you want only the name without the package:
def name = this.getClass.getSimpleName
See the documentation of java.lang.Class for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With