function - Can i have different return type in kotlin? -
for example let's call function this:
val call1: string = myfunction()
or
val call2: int = myfunction()
how can let function decide return in fonction of call , not function ? should return type ? ? unit ? ?
i don't know if it's clear here example on i'm working on:
i have line:
var query: query = firebasedatabase.getinstance().reference.child(path)
here query if that:
var query = firebasedatabase.getinstance().reference.child(path)
i'll databasereference
so i'm trying either query or databasereference according call of function , not function.
you can kind of fake it. in java it's feasible write method this:
public <t> t foo(class<t> clazz) { /* stuff */ }
that returns different things based on class pass in.
you'll still want have heavy lifting, can make interface bit nicer:
interface interface {} class clazz() : interface {} class clbzz() : interface {} fun <t: interface> foo(clazz: kclass<t>) : t { /* return t */ } inline fun <reified t: interface> foo() : t { return foo(t::class) } val = foo<clazz>() val b : clbzz = foo()
Comments
Post a Comment