https://kotlinlang.org logo
Title
s

Saiedmomen

08/23/2019, 6:38 PM
Can someone please tell me how I can see declarations of reflection types like
KFunction
and
KSuspendFunction1
? I have imported imported
kotlin-reflect
but I have to manually import them and IDE inspection tools like go to declaration and search in path can't find their declaration
m

Marc Knaup

08/23/2019, 6:40 PM
I think the compiler generates them on-the-fly as needed 🤔 What do you need the source for?
s

Saiedmomen

08/23/2019, 6:50 PM
I'm trying to findout why I can't pass a non suspending block to a higher order function that expects a suspend block. Wanted to read up a bit before posting it to discuss.kotlinlang
s

streetsofboston

08/23/2019, 6:55 PM
A
() -> T
type is not a ‘sub-class’ of (i.e. ‘assignable to’) a
suspend () -> T
type… They are entirely separate.
E.g.
var suspendLambda : suspend () -> Int = suspend { 5 }
var lambda : () -> Int = { 5 }

fun main() {
    suspendLambda = lambda // ERROR Type Mismatch
}
s

Saiedmomen

08/23/2019, 7:10 PM
yeah but I think any normal function is also a suspend function
s

streetsofboston

08/23/2019, 7:14 PM
No, not true… A normal function can be called inside a suspend function, a suspend function cannot be called in a normal function. This doesn’t mean that a normal function ‘is-a’ suspend function…..
But, I agree, it would be nice if there were some automatic conversions from
() -> T
to
suspend () -> T
by the compiler when needed. (like
() -> T
to
KFunction<T>
and such…)
m

Marc Knaup

08/23/2019, 7:15 PM
What are you actually trying to achieve? You can provide overloads for
suspend
and non-suspend functions.
There’s the same problem with passing a
suspend
function to a non-suspend argument of an inline function:
suspend fun doSomething(transform: suspend (Int) -> String) =
    listOf(1,2).map(transform)
Also doesn’t work 😕 But this works:
suspend fun doSomething(transform: suspend (Int) -> String) =
    listOf(1, 2).map { transform(it) }
Would be nice to have more automatic conversions indeed.
s

Saiedmomen

08/23/2019, 7:19 PM
yeah Anton. Thats exactly what I'd like to see. Sorry for my lack of fluency.
s

streetsofboston

08/23/2019, 7:20 PM
No need to be sorry 🙂 You have legitimate questions.