https://kotlinlang.org logo
Title
l

LastExceed

01/27/2022, 2:52 PM
val x: (suspend (Int) -> Int) = suspend { x: Int -> x * x }
even when specifying all the types explicitly i get this error:
Type mismatch.
Required: suspend (Int) → Int
Found: suspend () → Int
why?
m

Michael de Kaste

01/27/2022, 2:59 PM
because this is the signature of suspend itself:
public inline fun <R> suspend(noinline block: suspend () -> R): suspend () -> R = block
thus it cant accept any function with an input
l

LastExceed

01/27/2022, 3:06 PM
how do i fix my code then ?
s

smit01

01/27/2022, 3:12 PM
val x: suspend (Int)->Int) = { x:Int -> x*x }
m

Michael de Kaste

01/27/2022, 3:14 PM
depends on what you want, if you just mean a lambda (Int) -> Int, then simply
val x = { x: Int -> x * x }
would be enough
l

LastExceed

01/27/2022, 3:14 PM
thanks
s

smit01

01/27/2022, 3:15 PM
If I am not wrong don't mention suspend on right side.
l

LastExceed

01/28/2022, 1:21 PM