Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the swift underscore's official name?

Tags:

I have been trying to learn the names of the Swift features recently so that I can communicate with people with these names.

For example

  • If ! is added to the end of a type name, it's called an "implicitly unwrapped optional type"
  • guard denotes a "guard statement"
  • if let x = someOptional is called "Optional binding"
  • case 0...9: is an example of "pattern matching" in switch statements

But there is one feature that I don't know the name of.

Usually, I write this kind of code because C-style for loops are deprecated:

for _ in 0...9 {     print("hello") //print hello 10 times } 

I want to know what the underscore is called.

I know that this is from Ruby. So I think it will have the same name as the underscore in Ruby. So what exactly is the underscore called?

My guesses:

  • the underscore operator
  • the underscore
  • the underscore literal
  • the underscore identifier
  • the underscore to omit stuff

Is there an official term for this? If there isn't, how would I call this in a casual conversation about my code?

like image 678
Sweeper Avatar asked Apr 17 '16 13:04

Sweeper


1 Answers

It's called a wildcard pattern:

Wildcard Pattern

A wildcard pattern matches and ignores any value and consists of an underscore (_). Use a wildcard pattern when you don’t care about the values being matched against. For example, the following code iterates through the closed range 1...3, ignoring the current value of the range on each iteration of the loop:

for _ in 1...3 {    // Do something three times. } 

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/doc/uid/TP40014097-CH36-ID420

like image 173
Wouter J Avatar answered Oct 22 '22 07:10

Wouter J