[swift-evolution] Adding custom attributes

Radosław Smogura rsmogura at icloud.com
Sat Dec 26 08:59:04 CST 2015


What do you think about syntax like this:

// Declaration
@AttributeUsage(RUNTIME) //Can be SOURCE - attribute not emitted into binary
@AttributeTarget(PROPERTY) //Other CLASS, ENUM, METHOD, INIT
@attribute JSONProperty {
var name:String!;
var serializable:Bool? = true;
var deserializable:Bool? = true;

// Usage
var id:Int;

Attributes should be module aware (actual attributes should be redefined as Swift attribute, for beginning still can be hardcoded).

The attribute’s name have to be unique in module. It’s compile time error if attribute, class, enum, struct, etc has same name. 

Attribute’s properties are read only and can’t be assigned - so during reflection no-one would change ‘shared’ values.

Attribute’s properties can only support basic types: primitives, strings, types, and other attributes (optional). 

When declaring attributes, properties can be set to constant values (static let) or enum values, however the final value is stored in binary, not a reference to it.

The compiler has build in support for core attributes, which can affect code generation. Compiler may perform additional checks on core attributes.

Attributes on attributes (optional).

Best regards,

> On 26 Dec 2015, at 13:35, Dmitri Gribenko <gribozavr at gmail.com> wrote:
> On Sat, Dec 26, 2015 at 12:55 PM, Radosław Smogura
> <swift-evolution at swift.org> wrote:
>> Hello Swift Team,
>> As it’s my first post on this mailing list, let me introduce my self. My name is Radek Smogura, and for 10 year I work as developer, mainly Java and I know C and C++. I would like to join Swift community and work on Swift language. Previously for hobby I  added properties syntax to it and full closure support (you can find it here https://bitbucket.org/radoslaw_smogura/java-closures-silver-jdk9-langtools/wiki/Home).
>> I would like to propose custom attributes support, as it would make Swift more robust language, by allowing dependency injection and managed objects (ie. for Core Data or some trivial example like defining JSON structures for REST). I would as well to contribute to this feature to Swift language if possible as fork or branch.
> Custom attributes are an interesting direction that already came up on
> this list, but there was no concrete proposal with syntax and
> semantics.  Before you spend time implementing something extensive, I
> would strongly recommend you to float the idea on swift-evolution, and
> follow-up with a formal proposal if there is consensus.
> Dmitri
> -- 
> main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
> (j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/

More information about the swift-evolution mailing list