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 caseShawn
04/29/2019, 6:10 PMShawn
04/29/2019, 6:10 PMsimpleName
is String?
rather than String
, so you’ll need a default or !!
if you like living dangerouslyStefan Peterson
04/29/2019, 6:11 PMStefan Peterson
04/29/2019, 6:12 PMShawn
04/29/2019, 6:13 PMShawn
04/29/2019, 6:13 PMShawn
04/29/2019, 6:14 PMShawn
04/29/2019, 6:14 PMShawn
04/29/2019, 6:14 PMShawn
04/29/2019, 6:14 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
ladShawn
04/29/2019, 6:19 PMthis
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
Shawn
04/29/2019, 6:20 PMdata class B(override val name: String? = B::class.simpleName): A()
Shawn
04/29/2019, 6:21 PMthis
within the constructor because an instance hasn’t been instantiated yetShawn
04/29/2019, 6:24 PMtoString()
, 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