Unfamiliar Declaration Syntax

I was converting one of my apps to use unified logging, and in looking over the OSLog class, I saw something I had never seen before - a property declared with scare quotes around the name:

static let `default`: OSLog

example usage is:

os_log(.debug, log: .default, message, args)

So it looks like an anonymous enum is getting created. I looked through all the swift language docs until my eyes watered, and saw nothing like this mentioned.

Can someone explain what is happening here? And if it is documented somewhere I missed, a link to the relevant doc(s)?

The grave accents tell the compiler that it is an identifier. Most of the time you don’t need them, but if you are assigning something a name that happens to also be a keyword, then you can put them there to ensure the compiler won’t get confused:

var someVariable: Bool // ✓
var `someVariable`: Bool // ✓ (and does exactly the same thing)

var var: Bool // ✗
var `var`: Bool // ✓

var default: Bool // ✗
var `default`: Bool // ✓

It is worth noting that many keywords are contextual, meaning the compiler only looks form them in certain contexts. So sometimes a declaration will require them but the call site won’t or vice versa.

It is at least documented in the Language Reference under “Lexical Structure: Identifiers”. I don’t know if it is mentioned in the less technical Language Guide though.

There is no “anonymous enum” being created. The shorthand use of .default in os_log(.debug, log: .default, message, args) is called an “implicit member expression”:

An implicit member expression is an abbreviated way to access a member of a type, such as an enumeration case or a type method, in a context where type inference can determine the implied type. It has the following form:

. (member name)

For example:

var x = MyEnumeration.someValue
x = .anotherValue
GRAMMAR OF A IMPLICIT MEMBER EXPRESSION

implicit-member-expression. identifier

The dot notation accesses “a member of a type”, meaning a static or class property or method. The cases of an enum are static members of the enum type, and those are what you most often see accessed with dot notation. But, for example, you can also access CGRect.zero as just .zero in any context where the type is deduced to be CGRect. And in the code you're looking at, the type is deduced to be OSLog, so the dot notation looks for a static member named default in the OSLog type.