0

What does the `` around default mean in the following code below?

In what cases would you want to use it?

static let `default` = User(id: UUID(), name: "Anonymous")

Thank you in advanced

Nighthawk
  • 772
  • 11
  • 21

1 Answers1

5

The backticks allow you to use restricted keywords in places where they otherwise couldn't be used, such as variable names.

default is a restricted keyword in Swift, but by putting backticks around it, it becomes a valid variable name.

Without the backticks, your code would result in the error

Keyword 'default' cannot be used as an identifier here

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • 2
    This is the correct answer. However, I would advise against using restricted keywords in this way. It's confusing and makes your code harder to follow. I know about this language feature, but end up spending an embarrassing amount of time staring at code trying to figure out what the heck it's doing on those rare occasions when I encounter it. – Duncan C Nov 30 '20 at 18:31
  • @DuncanC couldn't agree more! – Dávid Pásztor Nov 30 '20 at 18:44