32

I would like to be able to annotate my types and methods with meta-data and read those at runtime.

The language reference explains how to declare attribute usages, but is it actually possible to declare your own attributes?

Reading would require some kind of reflection mechanism, which I was not able to find in the reference at all, so the second part of the question probably is - is there reflection possible. If these features are not available in Swift, can they be done with Objective-C code (but on Swift instances and types)?

A relatively unrelated note: The decision of what has been modelled as an attribute and what has been added to the core syntax strikes me as pretty arbitrary. It feels like two different teams worked on the syntax and on some attributes. E.g. they put weak and unowned into the language as modifiers, but made @final and @lazy attributes. I believe that once they actually add access modifiers, they will probably be attributes likes final. Is all of this somehow related to Objective-C interoperability?

Sebastian
  • 7,729
  • 2
  • 37
  • 69
  • Since you're running the same runtime as ObjC code, reflection should be somewhat possible. – Leandros Jun 04 '14 at 09:36
  • @Leandros: I am no ObjC expert at all - can you show us how? – Sebastian Jun 04 '14 at 10:46
  • IMHO it exist two different "types" e.g. weak is a ARC feature which llvm supports natively , other elementes, e.g. @objc is something on a higher level and i expect they use llvm meta data for this. – Stephan Jun 14 '14 at 05:52
  • But in general did you found a way to read at runtime the existing attributes? (I didn't search for it yet) – Stephan Jun 14 '14 at 05:56
  • @Stephan: No, I did not find a solution, nor did I really find the time to do so. – Sebastian Jun 23 '14 at 07:28

2 Answers2

10

If we take the iBook as definitive, there appears to be no developer-facing way of creating arbitrary new attributes in the way you can in Java and .NET. I hope this feature comes in later, but for now, it looks like we're out of luck. If you care about this feature, you should file an enhancement request with Apple (Component: Swift Version: X)

FWIW, there's really not a way to do this in Objective-C either.

ipmcc
  • 29,581
  • 5
  • 84
  • 147
  • Is it the same case with Swift 2.0? I heavily depend on Attributes (Annotations in Java) in my code especially when it comes to serialization/deserialization and ORM. I think custom attributes should be added to Swift – Kumait Jul 13 '15 at 02:08
  • 1
    ORM is arguably the *least* compelling case for attributes I can think of, being a persistence approach that is largely dismissed these days outside of legacy situations. I'm not saying that Swift shouldn't have attributes, but that ORM is not likely to be a compelling case for a "green field" language to add a feature. – ipmcc Jul 14 '15 at 10:10
8

You can now do something like this! Check out "Property Wrappers" - https://docs.swift.org/swift-book/LanguageGuide/Properties.html

Here's an example from that page:

@propertyWrapper
struct TwelveOrLess {
    private var number = 0
    var wrappedValue: Int {
        get { return number }
        set { number = min(newValue, 12) }
    }
}


struct SmallRectangle {
    @TwelveOrLess var height: Int
    @TwelveOrLess var width: Int
}

var rectangle = SmallRectangle()
print(rectangle.height)
// Prints "0"

rectangle.height = 10
print(rectangle.height)
// Prints "10"

rectangle.height = 24
print(rectangle.height)
// Prints "12"
Max Chuquimia
  • 7,494
  • 2
  • 40
  • 59
  • 5
    OK - there is a special case of attributes, now - exactly for the purpose of decorating properties, as far as I understand. But you still cannot read that meta-data at runtime through reflection. So this is neat, but sadly not of help at all for this question. While [they seem to agree with me](https://forums.swift.org/t/custom-attributes/13976) that this feature would be nice to have, it's still not available, I guess. – Sebastian Oct 18 '19 at 06:39