Does “let _ = …” (let underscore equal) have any use in Swift?

前端 未结 6 1462
野性不改
野性不改 2020-12-11 02:50

Does using let _ = ... have any purpose at all?

I\'ve seen question and answers for What's the _ underscore representative of in Swift References? a

6条回答
  •  长情又很酷
    2020-12-11 03:43

    Using let _ = ... specifically tells the compiler that you know that the expression on the right returns a value but that you don't care about it.

    In instances where the method has been marked with warn_unused_result, if you don't use the underscore then the compiler will complain with a warning. (Because in some cases it could be an error to not use the return value.)

提交回复
热议问题