https://kotlinlang.org logo
Title
m

Miguel

12/24/2018, 4:14 PM
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

Shawn

12/24/2018, 4:15 PM
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

Miguel

12/24/2018, 4:17 PM
But I what if I want to allow things like::
val x: Date = lol("13/02/2018", Date::class.java)
🤔
s

Shawn

12/24/2018, 4:18 PM
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
inline fun <reified T> lol()
m

Miguel

12/24/2018, 4:21 PM
I already searched that option but I call some private methods on that function so I can’t use it.
s

Shawn

12/24/2018, 4:25 PM
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

Miguel

12/24/2018, 4:33 PM
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

Shawn

12/24/2018, 4:38 PM
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

Miguel

12/24/2018, 4:41 PM
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

Shawn

12/24/2018, 5:00 PM
hm, I don’t think there’s a great way to do it without an unchecked cast at some point unfortunately
m

Miguel

12/24/2018, 5:13 PM
Oh I see. Thank you very much Shawn! Merry christmas if you celebrate it.
👍 1
s

Shawn

12/24/2018, 5:14 PM
No problem! Merry Christmas to you too
p

Pavlo Liapota

12/25/2018, 10:13 AM
@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

Miguel

12/25/2018, 11:00 AM
Thanks @Pavlo Liapota!
👍 1