简体   繁体   中英

Function literal with call-by-name parameter

How does one define an anonymous function with call-by-name parameters in scala?

I tried the following:

val fun = (x: Boolean, y: =>Int) => if(x) y else 0

This works well with call-by-value y, but not with call-by-name. Why?

它可以完成,但方式略有不同:与参数分开声明类型:

val fun: (Boolean, => Int) => Int = (x, y) => if (x) y else 0

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