robstoll
03/13/2019, 8:55 PMAny?
into its non-nullable variant?
I would like to avoid something like: fun <T: Any> foo(t: T?)
streetsofboston
03/13/2019, 8:57 PMfun <T : Any> foo(t: T?)
would do that, wouldn’t it?robstoll
03/13/2019, 8:57 PMAny
as upper boundpavel
03/13/2019, 8:58 PMstreetsofboston
03/13/2019, 8:58 PMfun <T> foo(t: T)
would be the one you want…pavel
03/13/2019, 8:59 PMfun <T:Any?> foo(t:T)
streetsofboston
03/13/2019, 8:59 PM: Any?
is the default if you omit itrobstoll
03/13/2019, 8:59 PMstreetsofboston
03/13/2019, 9:01 PMfun <T> foo(t: T){
if(t != null) bar<Any>(t) // no compile error
}
fun <T: Any> bar(t: T){}
pavel
03/13/2019, 9:04 PMfun <T : Any> foo(t: T?) {
if (t != null)
bar(t) //no compile error
}
fun <T : Any> bar(t: T) {}
works as wellstreetsofboston
03/13/2019, 9:04 PMbar
can no longer accept null-values….
I assumed that the signature of the functions can’t change, only their implementationpavel
03/13/2019, 9:05 PMfun <T: Any> bar(t: T){}
it never couldstreetsofboston
03/13/2019, 9:07 PMpavel
03/13/2019, 9:08 PMrobstoll
03/13/2019, 9:08 PMbar<Any>
doesn't help in my case because guess what, bar takes a second parameter:
fun <T: Any> bar(t: T, a: A<T>){}
streetsofboston
03/13/2019, 9:09 PMpavel
03/13/2019, 9:10 PMfun <T : Any> foo(t: T?) {
if (t != null)
bar(t, emptyList()) //no compile error
}
fun <T : Any> bar(t: T, f: List<T>) {}
robstoll
03/13/2019, 9:11 PMA
might not be covariantAny
makes sense 🙂fun <T : Any> foo(t: T?)
which I want to stay away from if possiblestreetsofboston
03/13/2019, 9:14 PMinterface A<T>
fun <T : Any> foo(t: T?) {
if (t != null)
bar(t, object: A<T>{}) //no compile error
}
fun <T : Any> bar(t: T, f: A<out T>) {}
And changing out
to in
or nothing still works fine.fun <T : Any> foo(t: T?)
?robstoll
03/13/2019, 9:15 PMstreetsofboston
03/13/2019, 9:16 PMfun <T : Any> foo(t: T?) : T
then this is a nice way of providing the generic, but since foo
doesn’t return a T type, you api gets a bit polluted.robstoll
03/13/2019, 9:17 PMbaz
which wants to use foo, now it has to use the workaround with T: Any
and T?
as well... but I guess I have to go this way until the new type inference is in placepavel
03/13/2019, 9:19 PMnull
params 😉robstoll
03/13/2019, 9:19 PMilya.gorbunov
03/13/2019, 11:16 PM