``` val closure = { (a: Int -> Int) -> a...
# announcements
i
Copy code
val closure = { (a: Int -> Int) ->
    a * 2
}
l
You can't. You can specify the type of
closure
explicitly though, and the IDE can even do that for you with an intention (alt/option + enter)
i
😕 type inference is failing
l
? Can you tell more details?
s
Copy code
val closure: (Int) -> Int = { a -> a * 2 }
is the closest you can get to what you’re asking for, I think
e
Expected type is one way. You can also use anonymous function literal instead:
Copy code
val closure = fun(a: Int): Int { ... }
s
@iex What is the type of
closure
if you would provide it explicitly?
You could end your last expression in your lambda with `as MyReturnType`….
E.g.
val closure = { a: Int -> (a + 5) as Number }
Is this what you try to accomplish?
i
yeah but I prefer to avoid casting, even in this case
managed to solve it with an extension function
s
Now i’m curious … 🙂
i
it's a bit too custom to post it I think...
declaring a local variable with the correct type and returning it also solved it, but that was 2 lines of code 😄
s
Providing the type explicitly is probably less typing … 🙂
i
that's basically what I'm doing with the extension
have a chain of map and flatmap which seemed to confuse the compiler
s
It could be that the new type inference system, that is still a bit in flux, may help with that.

https://www.youtube.com/watch?v=MyljSWm0Y_k

(fast forward to the type inference section)
i
thanks, will watch it later 👍