日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

浅谈 Scala 中下划线的用途

發(fā)布時(shí)間:2023/12/18 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 浅谈 Scala 中下划线的用途 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Scala 作為一門函數(shù)式編程語(yǔ)言,對(duì)習(xí)慣了指令式編程語(yǔ)言的同學(xué)來(lái)說(shuō),會(huì)不大習(xí)慣,這里除了思維方式之外,還有語(yǔ)法層面的,比如?underscore(下劃線)就會(huì)出現(xiàn)在多種場(chǎng)合,令初學(xué)者相當(dāng)疑惑,今天就來(lái)總結(jié)下 Scala 中下劃線的用法。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 1、存在性類型:Existential?types def?foo(l:?List[Option[_]])?=?... 2、高階類型參數(shù):Higher?kinded?type?parameters case?class?A[K[_],T](a:?K[T]) 3、臨時(shí)變量:Ignored?variables val?_?=?5 4、臨時(shí)參數(shù):Ignored?parameters List(1,?2,?3)?foreach?{?_?=>?println("Hi")?} 5、通配模式:Wildcard?patterns Some(5)?match?{?case?Some(_)?=>?println("Yes")?} val?(a,?_)?=?(1,?2) for?(_?<-?1?to?10) 6、通配導(dǎo)入:Wildcard?imports import?java.util._ 7、隱藏導(dǎo)入:Hiding?imports import?java.util.{ArrayList?=>?_,?_} 8、連接字母和標(biāo)點(diǎn)符號(hào):Joining?letters?to?punctuation def?bang_!(x:?Int)?=?5 9、占位符語(yǔ)法:Placeholder?syntax List(1,?2,?3)?map?(_?+?2) _?+?_??? 10、偏應(yīng)用函數(shù):Partially?applied?functions List(1,?2,?3)?foreach?println?_ 11、初始化默認(rèn)值:default?value var?i:?Int?=?_ 12、訪問(wèn)元組:tuple?getters t._2? 13、參數(shù)序列:parameters?Sequence? _*作為一個(gè)整體,告訴編譯器你希望將某個(gè)參數(shù)當(dāng)作參數(shù)序列處理!例如val?s?=?sum(1?to?5:_*)就是將1?to?5當(dāng)作參數(shù)序列處理。

?

這里需要注意的是,以下兩種寫(xiě)法實(shí)現(xiàn)的是完全不一樣的功能:

?
1 2 3 foo?_???????????????//?Eta?expansion?of?method?into?method?value foo(_)??????????????//?Partial?function?application

?

Example showing why foo(_) and foo _ are different:

?
1 2 3 4 5 6 7 8 trait?PlaceholderExample?{ ??def?process[A](f:?A?=>?Unit) ??val?set:?Set[_?=>?Unit] ??set.foreach(process?_)?//?Error? ??set.foreach(process(_))?//?No?Error }

?

In the first case, process _ represents a method; Scala takes the polymorphic method and attempts to make it monomorphic by filling in the type parameter, but realizes that there is no type that can be filled in for A that will give the type (_ => Unit) => ? (Existential _ is not a type).

In the second case, process(_) is a lambda; when writing a lambda with no explicit argument type, Scala infers the type from the argument that foreach expects, and _ => Unit is a type (whereas just plain _ isn't), so it can be substituted and inferred.

This may well be the trickiest gotcha in Scala I have ever encountered.

Refer:

[1]?What are all the uses of an underscore in Scala?

http://stackoverflow.com/questions/8000903/what-are-all-the-uses-of-an-underscore-in-scala

[2]?Scala punctuation (AKA symbols and operators)

http://stackoverflow.com/questions/7888944/scala-punctuation-aka-symbols-and-operators/7890032#7890032

[3]?Scala中的下劃線到底有多少種應(yīng)用場(chǎng)景?

http://www.zhihu.com/question/21622725

[4]?Strange type mismatch when using member access instead of extractor

http://stackoverflow.com/questions/9610736/strange-type-mismatch-when-using-member-access-instead-of-extractor/9610961

[5]?Scala簡(jiǎn)明教程

http://colobu.com/2015/01/14/Scala-Quick-Start-for-Java-Programmers/

轉(zhuǎn)載于:https://www.cnblogs.com/tonychai/p/4546264.html

總結(jié)

以上是生活随笔為你收集整理的浅谈 Scala 中下划线的用途的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。