Sweeper Sweeper - 2 years ago 73
Swift Question

What is the swift underscore's official name?

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?

Answer Source

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.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download