Apply method with curly braces

前端 未结 2 1391
感动是毒
感动是毒 2021-01-15 03:05

Why this works:

  List(
   \"string\", \"string2\"
  )

But this doesn\'t?

  List{
   \"string\", \"string2\"
  }

相关标签:
2条回答
  • 2021-01-15 03:48

    Canonical answer on braces and a supplement.

    The official glossary helpfully calls a block an encapsulation of side effects and a result value.

    A few opinions were recently expressed on the ML about braces and their non-pointy cousins.

    Perhaps it is unhelpful to think of parens and braces as somehow interchangeable in some contexts.

    It is easier to think in terms of exprs and blocks, and in this case, function args can be either comma-separated exprs or a block.

    0 讨论(0)
  • 2021-01-15 03:49

    The difference is in the comma -- you can only have a comma separated argument list when using parentheses.

    You will find that

    List {
        3
    }
    

    works just fine.

    0 讨论(0)
提交回复
热议问题