简体   繁体   中英

Is it possible to declare return type for closure?

I'm learning closure in scala programming language.

For example:

val a = (x:Int, y:Int) => x + y;
a(1, 2)

will give me 3 . the closure a works like a function (Int, Int):Int .

Is it possible to declare the return type for closure like this?

val a = (x:Int, y:Int):Int => x + y;
a(1, 2)

Is it possible?

This syntax is impossible ( val a = (x:Int, y:Int):Int => x + y ), but you can declare type for a :

val a: (Int, Int) => Int = (x, y) => x + y

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM