``` fun doSomething() { // return instance of wh...
# getting-started
t
Copy code
fun doSomething() {
  // return instance of whatever is between the angle brackets below
}

val x = <MyType>doSomething()
val y = <MyOtherType>doSomething()
How should the signature of doSomething look?
Thanks, but then I get a warning, Type mismatch, required Unit, found T… do I need to specify return type at the end somehow?
a
You should return T as well
Return type of the function should be T
t
Ah cool, and how would I reference the type inside the function? Eg, I want to pass it to something like
Copy code
return mapper.readValue<T>(xmlOut)
b
Copy code
inline fun <reified T> doSomething(): T {
  return T::class.constructors.toList()[0].call()
}
👍 1
🧠 1
t
👍 it worked - thank you!
k
This is a pretty strange thing to do, if you know the type you can just call the constructor yourself no?
b
Exactly, not sure what op is trying to achieve
t
Well, I’m actually passing the type on to a Jackson XML mapper - I’m just telling the function what type I want the XML mapped back to.
Looks like this in the end:
Copy code
private suspend inline fun <reified T> makeXmlRequest(req: Any): T {
        val xmlIn = mapper.writeValueAsString(req)
        val xmlOut = <http://client.post|client.post><String>(url) { body = xmlIn }
        return mapper.readValue(xmlOut)
    }
👍 1
k
Oh my bad, I thought you meant to actually call the constructor.
t
👍 no, I probably could have worded it better 🙂