https://kotlinlang.org logo
Title
i

iex

04/30/2019, 12:37 PM
is there a not so verbose way to define a class that contains all the fields from another + a new one (I don't care if it's via inheritance, composition)
j

Jonathan Mew

04/30/2019, 12:39 PM
is the class you want to add to
open
?
i

iex

04/30/2019, 12:39 PM
I can make it open yes
j

Jonathan Mew

04/30/2019, 12:41 PM
is extension too verbose?
open class A {}

class B(val a: Any): A()
i

iex

04/30/2019, 12:41 PM
my problem with verbosity is when
A
has a few fields
then you have to list them all in the constructor of B and pass to
A()
as well
j

Jonathan Mew

04/30/2019, 12:42 PM
sure, if they need passing in, they need passing in somewhere..
i

iex

04/30/2019, 12:42 PM
also, ideally
A
should be a data class...
(and
B
as well. I could also model both as case classes from a sealed class)
(and put the common fields in the sealed class)
j

Jonathan Mew

04/30/2019, 12:45 PM
hmmm does B want to be a data class also?
s

spand

04/30/2019, 12:45 PM
interface Notitication
interface ProgressNotification : Notification { 
val extra: Any
}

data class WithProgress(override val extra: Any, private val notification: Notification) : ProgressNotification, Notification by notification
Something like this ?
i

iex

04/30/2019, 12:46 PM
crossed my mind too, but I think that it doesn't help with verbosity... 🙈
as now the fields would have to be declared in
Notification
and then again in the default notification class...
j

Jonathan Mew

04/30/2019, 12:47 PM
data class A(val field: Any)

data class B(val a: A, val otherField: Any)
but then awkward to grab the internal fields 😞
i

iex

04/30/2019, 12:47 PM
in the later A and B aren't related though
I need to be able to match
j

Jonathan Mew

04/30/2019, 12:48 PM
I think the interface/delegation is probably your best bet.
👍 1
i

iex

04/30/2019, 12:48 PM
yeah was thinking that too. Will try it out
thank you!
j

Jonathan Mew

04/30/2019, 12:48 PM
but maybe someone more Kotlin-literate will have a better suggestion!
i

iex

04/30/2019, 12:48 PM
I doubt that there's anything else 😄
s

spand

04/30/2019, 12:48 PM
Either that or relax the requirement that
NotificationWithProgress
needs to be a
Notification
also and just pass it manually
i

iex

04/30/2019, 12:49 PM
yeah but then my notification parameter would have to be an "any"?
considering a function that accept these notifications as parameters
and matches on them
ok... trying out the delegate