gpt4 book ai didi

scala - 禁止在 Scala 中使用命名参数

转载 作者:行者123 更新时间:2023-12-02 19:57:22 25 4
gpt4 key购买 nike

Scala 中有没有办法禁止对函数使用命名参数?

示例:

def func(num: Int, power: Int) = math.pow(num, power)

func(3, 2) // OK
func(num = 3, power = 2) // Forbidden

最佳答案

您可以使用函数文字:

val func = (num: Int, power: Int) => math.pow(num, power)

func(3, 2)
func(num = 3, power = 2) // "error: not found: value num"

(尽管Function2apply仍然有参数名称:)

func(v1 = 3, v2 = 2)

关于scala - 禁止在 Scala 中使用命名参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19078669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com