https://kotlinlang.org logo
#random
Title
# random
v

vince

03/20/2023, 9:30 PM
Is there a way to delegate an interface by a private val instead of a constructor param? in a way that lets me access the delegate object in override functions my current options seem to be: 1. Use a default param - but I would really like to not allow
pool
to be specified externally
Copy code
class BitmapPool(size: Size, pool: Pool<Bitmap> = SynchronizedPool(SYNC_BUFFER_SIZE)) : Pool<Bitmap> by pool { ... }
2. initialize the delegate anonymously - but then I can't reference the delegate when overriding functions in the interface like
super
but not
Copy code
class BitmapPool(size: Size) : Pool<Bitmap> by SynchronizedPool(SYNC_BUFFER_SIZE) { ... }
a

asdf asdf

03/20/2023, 9:45 PM
You could make the primary constructor private, and then have a secondary constructor without delegate parameters. Default params/Weird companion object initializers have been the main ways i’ve found of doing it (aside from a compiler plugin intrinsic which is just too much hassle lol)
👍 1
v

vince

03/20/2023, 9:45 PM
ah that's a good idea (second constructor)
e

ephemient

03/20/2023, 9:56 PM
also you may be interested in https://youtrack.jetbrains.com/issue/KT-2747 or related tickets
👀 2
a

asdf asdf

03/20/2023, 10:37 PM
That would be really nice to have
Wish that you were also able to refer to
this
even if its somewhat unsafe
6 Views