Hello! Can someone tell me why can’t I do this?
# getting-started
m
Hello! Can someone tell me why can’t I do this?
Since I am defined a generic return type, shouldn’t I be allowed to return anything?
s
No, that’s not how generics are used.
If you want to return anything, your return signature would be
List<Any>
a generic constraint allows you to return the type specified by the enclosing class or at the call site
m
But I what if I want to allow things like::
val x: Date = lol("13/02/2018", Date::class.java)
🤔
s
That doesn’t contradict what I’ve said. If
lol()
returns a different type based on the class you pass to it, you can still use generics
for a
fun <T> lol(): T
, you’d get a
Date
out of it by calling like this:
lol<Date>()
if you need the
Class<T>
from it, use
reified
Copy code
inline fun <reified T> lol()
m
I already searched that option but I call some private methods on that function so I can’t use it.
s
Well, you could potentially delegate out to another public method that isn’t
inline
, but the better question at this point really is what are you trying to do?
m
What I want to achieve is this: I want to build a collection of objects, by parsing a string representation of them, that has a fixed length. In this case I want to be able to parse
(2, 2)
-> Int and
(27/03/2017, 28/03/2017)
-> LocalDate. Ideally I would want a function
fun <T> fromString(elementsString: String, classValue: Class<T>): List<T>
that receives the string representation and returns the list of elements, which can be a list of 2 Ints or a list of 2 LocalDates.
That sort of explains the image I sent you. I have another 2 functions that return a nullable type indicating if they could parse 2 ints or 2 dates respectively. So in my head, I’m imagining the
fromString
function to call the “parseInt” function and returning that, and only calling the “parseDate” if the other one was not well succeeded.
sorry if it wasn’t very clear at first what I was trying to achieve
s
how do you plan on calling this function? If you know what the types are beforehand, why not just overload the function - have one that accepts two dates (strings?) and one that accepts two ints
m
Ideally I would plan on calling like
val x: List<Int> fromString("[2,2]", Int::class.java)
Well , I do the know the types beforehand but that would originate repeated code for every type I would want to implement. There is a core logic that I kind of want to maintain in
fromString
and I would like to delegate the responsibility of parsing to the wanted type to some other function.
s
hm, I don’t think there’s a great way to do it without an unchecked cast at some point unfortunately
m
Oh I see. Thank you very much Shawn! Merry christmas if you celebrate it.
👍 1
s
No problem! Merry Christmas to you too
p
@Miguel, I agree with @Shawn that if you know the type, then it is better to have separate function for each type supported by you. But all common logic during parsing you can have in one generic function, so no code duplication. See example here: https://pl.kotl.in/rkWaMYyWE
m
Thanks @Pavlo Liapota!
👍 1