03/24/2022, 9:59 PM
fun main() {
    var newList1 = listOf(listOf(1, "a", "b"), listOf(1, "a", "b"), listOf(1, "a", "b"))
    var newList = listOf(1, "b", "C" to newList1)
    var defaultList = listOf(1, "a", 'B' to newList)

    var a = defaultList[2]

	print(  //kotlin.Pair
I need to access "newList1" but i cant since "defaultList" is a Pair
how can i access via index or how can i construct to get this behaviour


03/24/2022, 10:41 PM
(a as Pair<Char, List<Any>>).second
of course, casts are evil. So hopefully your real code does have better typed and homogeneous data structures
btw: the correct channel for this would be #getting-started
and no,
is not a Pair: it is a
(in you concrete example, a list consists of an Int, a String, and a Pair of Char and List) . Thus the "static" type of the list entries is
(the supertype of all your item types), but individual items have different runtime types (which is why the cast is necessary)
☝️ 1


03/24/2022, 11:06 PM
fun main() {
    data class Quote(
    	var int: Int,
        var item: String,
        var ItemList: Pair<String, List<Any>>
    var newList1 = listOf(
        Quote(1, "asds dd", "sd dsdb" to emptyList()), 
        Quote(1, "adsd sdssdsds", "bsdds sdd" to emptyList()), 
        Quote(1, "asdsd sds", "bdsd sd" to emptyList())
    var newList = listOf(
        Quote(1, "bsds sdsds", "Cdfdd fsd" to newList1)
    var defaultList = listOf(
        Quote(1, "aasds ss", "Bdsf wsd" to newList)

    var a = defaultList[0] //as Pair<Char, List<Any>>).second;

// 	print(
    print( // kotlin.Pair
still a kotlin.Pair i dont know why

Richard Gomez

03/24/2022, 11:23 PM
What are you expecting? ItemList is
because you've defined it as such:
var ItemList: Pair<String, List<Any>>.


03/24/2022, 11:35 PM
i got it now i need to use ".second" on the Pair to get the list

Richard Gomez

03/25/2022, 12:35 AM
In general, that seems like a bad design for data. Why not have
Map<String, List<Any>>

Matteo Mirk

03/25/2022, 9:06 AM
Don’t use Pair to model your data, it won’t add any semantic meaning to your code, quite the opposite, only confusion. Pair is handy for when you need to quickly return 2 values from a function or you need an intermediate structure in a functional pipeline. Model your data structures using concepts and terms from the domain you need to model. Use the “Ubiquitous Language” as in Domain Driven Design.
👍 2