Scala具名参数Named Argument
admin
2023-08-03 13:20:09
0次

什么是具名参数呢?

下面通过简单例子来说明。

需求:有一个42岁的父亲和一个12岁的孩子,求父亲比孩子年长多少岁?可以很简单的写出下面的函数体来实现,如下图:

Scala具名参数Named Argument

特殊情况来了,如果在使用getDvalue函数时,不小心导致了参数p和c写颠倒了,那么结果会是下图所示:

Scala具名参数Named Argument

这样就不是所要的结果了,很小的疏忽导致了结果的不正确性,做一下简单的修改,如下图所示:

Scala具名参数Named Argument

结果又是我们最开始所需的了。

下面写几个错误的示例:

Scala具名参数Named Argument

code:

package demo

object NamedArgument {

  def main(args: Array[String]): Unit = {

    val parent=42

    val child=12

    def getDvalue(p:Int,c:Int):Int={

      p-c

    }

   println("d-value is :"+getDvalue(c=child,p=parent))

   

   def testNA(a:Int,b:String)={}

   testNA(1,"string")//right

   testNA("string",1)//Multiple markers at this line:

                     //type mismatch; found : String("string") required: Int

                     //type mismatch; found : Int(1) required: String

                     //如果不使用具名参数,调用函数时应按照函数定义的参数顺序填写参数

   testNA(b="string",a=1)//right 具名参数

   testNA(b="string",1)//positional after named argument.

                       //如果函数中某个参数没有具名,则按照其所在位置指定参数,可以叫做位置参数

   testNA(b=1,a="string") //Multiple markers at this line:

                          //type mismatch; found : String("string") required: Int

                          //type mismatch; found : Int(1) required: String

                          //type mismatch; found : String("string") required: Int

                          //具名参数指定类型错误,这是比较明显的错误

   testNA(1,a="string") //parameter 'a' is already specified at parameter position 1

                        //参数a已经在位置1(a="string")定义过,与位置参数1(所在位置0)冲突

   testNA(1,b="string") //right

   testNA(a=1,"string") //right

   

  }

}

接下来试试其它的类型

   def testNA2(a:Int*,b:String)={}  //*-parameter must come last  这是基本语法的约束,可变参数必须放在最后,这种写法是错误的

   def testNA2(a:String,b:Int*)={}  //right

   testNA2("string",1,2,3,4)//right

   testNA2(a="string",b=1,2,3,4)//right

   testNA2(1,2,3,4,"string")//type mismatch; found : Int(1) required: String

                            //type mismatch; found : String("string") required: Int

   testNA2(b=1,2,3,4,a="string")

   testNA2(b= List(1,2,3,4),a="string")//type mismatch; found : List[Int] required: Int  

                                       //这样会提示类型错误,这样写其实就不是可变参数了 

                                       //可以写成如下形式testNA3

   def testNA3(a:String,b:List[Int])={}

   testNA3(b= List(1,2,3,4),a="string")

   

   //匿名参数

   def testNA4(a:Int,b:String)=a

   val t1: Int=>Int = testNA4(_:Int,"string")

   val t2: Int => Int = testNA4(b = "string", a = _) //positional after named argument.

   val t3 = testNA4(b = "string", a = _: Int) //positional after named argument.

   val t4: Int=>Int = testNA4(_ , b="string") //right

   val t5: Int=>Int = testNA4(_ , "string") //right   

   val t6: Int=>Int = testNA4(a=_ , b="string") //type mismatch; found : Int required: Int=>Int

   //匿名参数不能使用具名参数


相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...