https://kotlinlang.org logo
#getting-started
Title
# getting-started
c

Christian Sousa

07/22/2020, 1:40 PM
Hey guys! Anyone knows what should something like this be implemented in Kotlin?
Copy code
export interface Item {
  [label: string]: string[];
}

export interface SequenceMap {
  '1': string[] | Item ;
  '2': string[] | Item ;
}
I know that I can use a
sealed class
to mimic the
string[] | Item
. But what is really bugging me is how can I do something like the Item interface? I’m not that used to Kotlin but this is something somewhat trivial in Typescript. Any help would be great! Thanks!
r

Rob Murdock

07/22/2020, 1:47 PM
Am I correctly understanding that you’re looking for a way to use an arbitrary string to ask Item for a value?
c

Christian Sousa

07/22/2020, 1:48 PM
Yeah, it might be impossible on Kotlin right?
that’s probably as close as you’ll get
c

Christian Sousa

07/22/2020, 1:49 PM
And even with the object SequenceMap using the named entries ‘1’ and ‘2’, that’s something that I can’t do on kotlin, or am i missing something?
r

Rob Murdock

07/22/2020, 1:49 PM
if you don’t mind putting the names of the properties in sourcecode, there are “quick” ways of having properties that do that in the background
is SequenceMap supposed to extend Item?
or does it just have two properties
c

Christian Sousa

07/22/2020, 1:50 PM
just has two properties
r

Rob Murdock

07/22/2020, 1:51 PM
I’d declare them as explicit properties then
Copy code
class SequenceMap() {
 val one get(): NodeSealedParentType = ...
 val two get(): NodeSealedParentType = ...
}
i suspect you won’t be able to do a true map.1 property, but I hope that’s a small sacrifice?
c

Christian Sousa

07/22/2020, 1:52 PM
yeah, I’d have to work around that 😛
r

Rob Murdock

07/22/2020, 1:52 PM
you could do it as a data class too
haha let me guess, you’re kiiind of using them as a js common interface, without any explicit commonality
c

Christian Sousa

07/22/2020, 1:52 PM
Because I will have some classes implementing that interface
r

Rob Murdock

07/22/2020, 1:53 PM
ah so that interface is trying to enforce that the child has elements for those properties
c

Christian Sousa

07/22/2020, 1:53 PM
Yeah it is
r

Rob Murdock

07/22/2020, 1:53 PM
typescript, amirite
c

Christian Sousa

07/22/2020, 1:53 PM
Yup
But I think that having the sealed class for those two types will work i think
r

Rob Murdock

07/22/2020, 1:55 PM
I would probably have an interface that enforces two properties exist … maybe do something fun with delegate properties on the implementing classes
maybe add another interface or static function that takes a key and always returns property 1 or property 2 from implementers of the 1-2 interface
you’ll have to toy with it a bit to ensure you get the value from the types originally intended I expect
c

Christian Sousa

07/22/2020, 1:58 PM
Do you know of some resource online that I could take a look into for a similar solution?
r

Rob Murdock

07/22/2020, 2:00 PM
not sure what you’re really trying to do with that type modeling, so can’t really help on that side (maybe some multimap libraries?) but for stuff like operators and delegate properties the kotlin docs are pretty good. https://kotlinlang.org/docs/reference/delegated-properties.html
t

Tobias Berger

07/22/2020, 3:10 PM
Why not just use a Map structure for Item?
3 Views