Scala's tuple unwrapping nuance

后端 未结 3 2031
轻奢々
轻奢々 2021-02-02 14:57

I\'ve noticed the following behavior in scala when trying to unwrap tuples into vals:

scala> val (A, B, C) = (1, 2, 3)
:5: error: not found: va         


        
3条回答
  •  太阳男子
    2021-02-02 15:45

    A workaround exists if you need to initialize a large number of constants and want to avoid writing val = for each or you are hitting the tuple size limit (22).

    From section 4.1 of The Scala Language Specification:

    A value definition val x: T = e defines x as a name of the value that results from the evaluation of e. A value definition val p1, ..., pn = e is a shorthand for the sequence of value definitions val p1 = e; ...; val pn = e.

    Per the specification, one can initialize a sequence of values all with names starting with capital letters by specifying an expression on the right-hand which returns each value in order.

    val iter = Iterator(1, 2, 3)
    val A, B, C = iter.next()
    

    Another example:

    val next = { var n = 0; () => { n = n + 1; n } }
    val A, B, C, D, E, F, G, H = next()
    

    In these trivial cases above this approach is not very useful. Below is a more useful example that initializes a constant for each of the 64 squares of the chessboard (see Square.scala#L31 for source):

    val squareIter = squares.iterator
    val A1, A2, A3, A4, A5, A6, A7, A8,
      B1, B2, B3, B4, B5, B6, B7, B8,
      C1, C2, C3, C4, C5, C6, C7, C8,
      D1, D2, D3, D4, D5, D6, D7, D8,
      E1, E2, E3, E4, E5, E6, E7, E8,
      F1, F2, F3, F4, F5, F6, F7, F8,
      G1, G2, G3, G4, G5, G6, G7, G8,
      H1, H2, H3, H4, H5, H6, H7, H8 = squareIter.next()
    

提交回复
热议问题