Hey guys! Anyone knows what should something like ...
# getting-started
c
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
Am I correctly understanding that you’re looking for a way to use an arbitrary string to ask Item for a value?
c
Yeah, it might be impossible on Kotlin right?
that’s probably as close as you’ll get
c
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
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
just has two properties
r
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
yeah, I’d have to work around that 😛
r
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
Because I will have some classes implementing that interface
r
ah so that interface is trying to enforce that the child has elements for those properties
c
Yeah it is
r
typescript, amirite
c
Yup
But I think that having the sealed class for those two types will work i think
r
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
Do you know of some resource online that I could take a look into for a similar solution?
r
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
Why not just use a Map structure for Item?