KeyPath syntax with $

On Chapters 7, 8 we KeyPaths were used but I’m a bit confused by the $ symbol.
I have a basic understanding of KeyPaths in Swift but I haven’t seen something like this.
Is this unique to Vapor?

Acronym.query(on: req.db).sort(\.$short, .ascending).all()

To be clear, I understand the functionality, I more so want to understand how the syntax works.

That’s the property wrapper projected value. Have a look at Properties — The Swift Programming Language (Swift 5.7) under Projecting a Value From a Property Wrapper

Essentially it allows us to access the property wrapper rather than the value which gives us access to things like the column name and whether the value is eager loaded etc, stuff we can’t access using the property itself

1 Like

Thanks @0xtim for putting a name to it :sweat_smile:

If I understand correctly simply using “the value” here wouldn’t be enough because we are dealing with a database so we need access to the property wrapper which holds information required to perform such a query.

The projected property type here is FieldProperty<Acronym, String>.
I think the confusing part here for me was/is the concept of projected values.
Since it seems like a projected value can be any type and in turn projected values are used differently. Which requires me to understand more about the Field property wrapper and its related types.

That’s correct. You can view the source for the @Field property wrapper at fluent-kit/Field.swift at main · vapor/fluent-kit · GitHub

1 Like