Fast variable name with `(backtick) character

I looked through Alamofire sources and found a variable whose name is referenced in this source file

open static let `default`: SessionManager = { let configuration = URLSessionConfiguration.default configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders return SessionManager(configuration: configuration) }() 

However, in places where the variable is used, there are no backlinks. What is the purpose of the reverse steps?

+5
source share
3 answers

According to the Swift documentation :

To use a reserved word as an identifier, set it before and after it. For example, a class is not a valid identifier, but `class` is valid. Backlinks are not considered part of the identifier; `x` and x have the same meaning.

In your example, default is a quick reserved keyword, so backlinks are needed.

+10
source

Simply put, using return outputs, you can use reserved words for variable names, etc.

 var var = "This will generate an error" var `var` = "This will not!" 
+3
source

An example of adding to the accepted answer regarding the use of reserved word identifiers after they have been correctly declared using backlinks.

... Backticks are not considered part of the identifier; x and x have the same meaning.

Meaning we do not need to worry about using backticks after declaring an identifier (however we can):

 enum Foo { case `var` case `let` case `class` case `try` } /* "The backticks are not considered part of the identifier; `x` and x have the same meaning" */ let foo = Foo.var let bar = [Foo.let, .`class`, .try] print(bar) // [Foo.let, Foo.class, Foo.try] 
+2
source

Source: https://habr.com/ru/post/1262412/


All Articles