https://kotlinlang.org logo
i

igor.wojda

02/09/2021, 12:41 PM
I must admit my mind was blown 🤯 Who can explain to me what’s exactly is going on here?
😍 5
d

Dmitry

02/09/2021, 12:47 PM
Class
Survey
is created. It is extended from List, because author want it to have all the functionality that list have. But we don’t want to implement that all by ourselves. So we delegate all of those methods on
List
interface that to actual list with
by
keyword.
👍 3
☝️ 1
This allows us to have our own type “Survey” that we can use accross project, while having all the goodies of it being also a “List” without manually delegating all methods from List interface to underlying list.
👍 10
Alternative would be (if you are more of a show me a code guy):
(and so on, manually delegating everything by-hand to private property)
m

Matteo Mirk

02/09/2021, 1:16 PM
Igor you may want to read about delegates: https://kotlinlang.org/docs/reference/delegation.html
w

wbertan

02/09/2021, 1:27 PM
Besides the class
Survey
also guarantee it will have at least one item in the list.
v

Vampire

02/09/2021, 1:54 PM
And that it is a read-only list. The result of
listOf
could easily be cast to
MutableList
and modified usually.
a

Animesh Sahu

02/09/2021, 2:11 PM
The result of 
listOf
 could easily be cast to 
MutableList
 and modified usually.
Atleast on JVM, I remembered,
listOf()
returns
Arrays.asList
which throws runtime exceptions if you try to add or remove something. However it does let you replace items.
v

Vampire

02/09/2021, 2:13 PM
Indeed, it's a fixed size list
j

juliocbcotta

02/11/2021, 12:21 PM
That is a bad implementation that gets a lot of attention. Are you sure that you want to have all API from list in your survey? You most likely are exposing too much API. The version with a NonEmptyList class as parameter allow a much more elegant solution where you have control of the exposed API
1
2 Views