https://kotlinlang.org logo
#announcements
Title
# announcements
t

thiagoretondar

07/10/2019, 2:23 PM
Is it a good practice?
Copy code
data class Property(
    val name: String,
    val value: String
)

data class Element(
    val name: String,
    val properties: List<Property>?,
    val elements: List<Element>?  ---> REFERENCE ITSELF
)
d

dalexander

07/10/2019, 2:27 PM
Also I would just use a
Map<String, String>
instead of a
List<Property>
unless
Property
is a placeholder for something more complex than two strings.
r

Ruckus

07/10/2019, 2:29 PM
@dalexander That would be semantically different. There's no indication
name
can't repeat, or it may just be that order is important and uniqueness is handled manually.
👌 1
d

dalexander

07/10/2019, 2:29 PM
You at-ed the wrong person. But I suppose that's true.
r

Ruckus

07/10/2019, 2:30 PM
Yeah, the list updated right before I hit enter 😠
d

dalexander

07/10/2019, 2:30 PM
Generally something called "properties" can't / doesn't want to have duplicates though, which is why I added that.
r

Ruckus

07/10/2019, 2:40 PM
Agreed. I actually have a similar issue in a project I'm working on where I have to use a List and handle uniqueness manually as being able to change the order is important, which is why it came to mind.
h

Hullaballoonatic

07/10/2019, 5:12 PM
Also a data class comes with field names for the "key" and "value" which is syntactically pleasing 😉
r

Ruckus

07/10/2019, 5:14 PM
So does
Map.Entry
, or am I missing your point?
h

Hullaballoonatic

07/10/2019, 5:16 PM
Instead of
entry.key
you have
property.name
which is more context.
r

Ruckus

07/10/2019, 5:17 PM
Ah, I see what you're saying. Sorry about that 🙂
h

Hullaballoonatic

07/10/2019, 5:19 PM
I could have explained it better 🙃
When I use maps I always feel I have to write really verbose variable names
Copy code
val firstNameToNameOfFavoritePet: Map<String, String> = ...
🤔 1