Stefan Peterson
04/29/2019, 6:08 PMself
or something equivalent in a constructor of a data class such that you could do something like:
abstract class A(abstract name: String?)
data class B(override val name: String? = self::class.simpleName): A()
val b = B()
print(b.name) // B
Shawn
04/29/2019, 6:10 PMthis
in this casesimpleName
is String?
rather than String
, so you’ll need a default or !!
if you like living dangerouslyStefan Peterson
04/29/2019, 6:11 PMShawn
04/29/2019, 6:13 PMabstract class A {
abstract val name: String?
}
data class B(override val name: String? = this::class.simpleName): A()
is validbloder
04/29/2019, 6:16 PMthis
but is showing me that this is not defined in this context
, are u getting to do that with this
?Shawn
04/29/2019, 6:17 PM.kt
ladthis
evaluates just fine within the scratch file because it resolves to the outer class that the scratch file code technically resides inbloder
04/29/2019, 6:19 PMShawn
04/29/2019, 6:20 PMthis
data class B(override val name: String? = B::class.simpleName): A()
this
within the constructor because an instance hasn’t been instantiated yettoString()
, equals()
, hashCode()
, copy()
, and property-only constructors). they act just like regular classes in this instance and you should assume they do in most casesStefan Peterson
04/29/2019, 9:17 PMB::class.simpleName
was just hoping for something a little nicer. Thanks all. And yea I am using data class
here specifically for the copy
feature