美文网首页scala
scala issue: tuple assignment to

scala issue: tuple assignment to

作者: jane456 | 来源:发表于2017-08-12 23:02 被阅读0次

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_=>}

相关文章

网友评论

    本文标题:scala issue: tuple assignment to

    本文链接:https://www.haomeiwen.com/subject/mqcsrxtx.html