# Pitch: Introduce custom attributes

**URL:** <https://forums.swift.org/t/pitch-introduce-custom-attributes/21335>\
**Category:** Pitches\
**Created:** [March 7, 2019, 5:21pm UTC](https://forums.swift.org/t/pitch-introduce-custom-attributes/21335 "2019-03-07T17:21:28Z")\
**Posts on this page:** 1\
**Showing post:** 27

<div class="post-metadata">

**Author:** ![hartbit](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hartbit/32/738_2.png) [@hartbit](https://forums.swift.org/u/hartbit)\
**Post date:** [March 20, 2019, 8:44am UTC](https://forums.swift.org/t/pitch-introduce-custom-attributes/21335/27 "2019-03-20T08:44:50Z")

</div>

> [@Vinicius\_Vendramini](#):
>
> I've come to agree with the sentiment that including custom attributes that are available at runtime might be a bit much. Therefore, I'll try to make a first proposal only for compile-time attribtues. I don't really understand the steps in the Swift compiler, but it seems to me that attributes should at least be available in libSyntax and SourceKit so that tools can officially deal with them. Any clarifications here are appreciated.

It makes sense to simplify this proposal by relegating runtime access to a future proposal, but I think its still important that we think about them. For example, I've always really liked the fact that C# Custom Attributes are defined as standard types and their constructors:

- it doesn't introduce a new syntax
- those types serve both as definition and as runtime metadata
- behaviour can be attached to them

Here's what it could look like in Swift:

```swift
@attribute(name: "codingKey", usage: .variables)
internal struct CodingKeyAttribute {
    let key: String

    init(key: String = "key") {
        self.key = key
    }

    var snakeCase: String {
        // ...
    }
}

@attribute(name: "ignoreRules", usage: [.functions, .variables, .classes])
internal struct IgnoreRulesAttribute {
    private let rules: [String]
    private let printWarnings: Bool

    init(_ rules: [String], printWarnings: Bool = false) {
        self.rules = rules
        self.printWarnings = printWarnings
    }
}

```

Once we allow accessing them at runtime, we could reuse the type as metadata:

```swift
for attribute in myObject.reflectionMetadata.attributes {
    if let codingKey = attribute as? CodingKeyAttribute {
        print(codingKey.snakeCase)
    } else if let ignoreRules = attribute as? IgnoreRulesAttribute {
        // Ignore
    }
}

```

---

_[View the full topic](https://forums.swift.org/t/pitch-introduce-custom-attributes/21335)._
