user3773048
user3773048

Reputation: 6239

Swift: What does backslash dot "\." mean?

I'm new to backend Swift and thought I'd use Vapor to get up-and-running on a side project fast... I ran vapor new WebServer --template=auth-template, and now I'm trying to figure out what something like return \.email means. For more context, I'm looking in WebServer > Sources > App > Models > Users.swift:

import Authentication
import FluentSQLite
import Vapor

/// Allows users to be verified by basic / password auth middleware.
extension User: PasswordAuthenticatable {
    /// See `PasswordAuthenticatable`.
    static var usernameKey: WritableKeyPath<User, String> {
        return \.email
    }

// ...
}

And here is the definition of the User class:

/// A registered user, capable of owning todo items.
final class User: SQLiteModel {
    // {omit extra code} ...

    var email: String

    // {omit extra code} ...

    /// Creates a new `User`.
    init(id: Int? = nil, name: String, email: String, passwordHash: String) {
        // {omit extra code} ...
        self.email = email
        // {omit extra code} ...
    }
}

What does this backslash-dot notation mean?

Upvotes: 70

Views: 17915

Answers (2)

yoAlex5
yoAlex5

Reputation: 34215

[Swift Key Value Coding (KVC)]

This syntax(\<path_to_type_property>) is used to specify KeyPath and allows to check this path in compile time(Objective-C used String to specify it).

What we can get from it:

//`WritableKeyPath` allows in addition to read also write to this property 
//Value Path is User.email
//Root Type is User
//Value Type is String as a result of .email 
var usernameKey: WritableKeyPath<User, String> = \.email
//or
var usernameKey: WritableKeyPath<User, String> = \User.email

How it can be used like:

//read
let userEmail = someUser[keyPath: usernameKey]

//write
someUser[keyPath: usernameKey] = "[email protected]"

Upvotes: -1

user3773048
user3773048

Reputation: 6239

tl;dr: We take a look at the Swift language reference, and sure enough, the usage of this backslash-dot notation is called a key-path-expression.

(The question has been sufficiently answered, by this point.)

A more hands-on approach on how to get to that piece of buried documentation:

As you can see from the code you posted, the User class contains a property named email.

Notice that, assuming you're using Xcode, if you replace return \.email with return \, you get the compile-error "Expected expression path in Swift key path", so this is a hint that this backslash-dot notation might have to do with something called a key path.

From that documentation on key-path, we see that we could also have written \User.email (and you can try it out in Xcode with no compiler error).

Understanding the greater context of what's going on in that code:

So, semantically, to understand the meaning of the usernameKey declaration you're looking at, we might want to understand what a WritableKeyPath is. In simple, from the documentation, we see that a WritableKeyPath is: "A key path that supports reading from and writing to the resulting value."

So, we see that the usernameKey declaration takes in a WritableKeyPath object and returns a String that is User.email.

Furthermore, it's apparent that the User class needs this usernameKey property in order to conform to the PasswordAuthenticatable protocol, which was imported on the first line with import Authentication (if you care to explore there, take a look at Dependencies > Auth 2.0.0 > Authentication > Basic > BasicAuthenticatable.swift).

Upvotes: 62

Related Questions