scala issue: tuple assignment to variables in Scala

https://stackoverflow.com/questions/6196678/is-it-possible-to-have-tuple-assignment-to-variables-in-scala

This isn't simply "multiple variable assignment", it's fully-featured pattern matching!

So the following are all valid:

val (a,b)=(1,2)valArray(a,b)=Array(1,2)

val h::t=List(1,2)

val List(a,Some(b))=List(1,Option(2))

This is the way that pattern matching works, it'll de-construct something into smaller parts, and bind those parts to new names.  As specified, pattern matching won't bind to pre-existing references, you'd have to do this yourself.

var x:Int=_vary:Int=_

val (a,b)=(1,2)x=a

y=b// or(1,2) match {case (a,b)=>x=a; y=b case_=>}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,932评论 0 23
  • 水龙吟·梦幻人间 弯弯孤月凝眸 荒荒草色寒风祭 几杯凉酒 一袭冷影 无根颤栗 倦旅归路 春秋之所 何人熟记 梦幻人...
    萧瀹阅读 311评论 1 9
  • 亲爱的,此刻你一定幸福的和家人一起聊着我,那我也和你聊聊我吧。 亲爱的,清晨,高速上,咱俩往常一样聊天谈笑,你突然...
    柠檬蜜夫人阅读 661评论 8 6
  • 一潭湖水映天上的月亮映身旁的火光映我似满脸皱纹的模样为此我投颗石子想看到十八岁的面庞十八岁的面庞却粉碎得像一张网网...
    何处有嘉木阅读 232评论 2 3
  • Swift算法俱乐部:Swift 链表数据结构@(Swift)在本教程中,我们将会在Swift 3中实现链表。##...
    刘铭iOS阅读 299评论 0 1