Custom annotation processors?

As a former Googler, I've spent a lot of years writing Java code that uses
dependency injection, and this relies heavily on the ability to have custom
annotations/attributes in the language - particularly, user-defined
attributes on function parameters - and to generate additional code at
compile time via annotation processors. Although dependency injection does
have it's detractors, it's getting better (current best of breed is
http://google.github.io/dagger/\), and it solves an amazing array of
problems, including the ability for asynchronous programming to disappear
into the underlying framework - you just write synchronous code and the
framework handles the rest (no more futures!).

Now, you can of course do dependency injection without custom attribute
support in the language, but it's much more cumbersome. The user-defined
attributes allow you to specify, in a simple declarative way, the runtime
dependencies between various classes. Without it you have to build up those
dependencies in code, using some sort of fluent interface or builder
pattern.

So my question is, is there any plan for Swift to support user-created
annotations, and annotation processing compilation stages?

···

--
-- Talin

Hi Talin,

We have no concrete plans for user defined attributes, but it is a natural extension. One of our goals for Swift 3 is to nail down the reflection metadata representation. We should design this to be extensible to support user defined attributes so that we don’t close this off in the future.

-Chris

···

On Jan 13, 2016, at 5:24 PM, Talin via swift-evolution <swift-evolution@swift.org> wrote:

As a former Googler, I've spent a lot of years writing Java code that uses dependency injection, and this relies heavily on the ability to have custom annotations/attributes in the language - particularly, user-defined attributes on function parameters - and to generate additional code at compile time via annotation processors. Although dependency injection does have it's detractors, it's getting better (current best of breed is http://google.github.io/dagger/\), and it solves an amazing array of problems, including the ability for asynchronous programming to disappear into the underlying framework - you just write synchronous code and the framework handles the rest (no more futures!).

Now, you can of course do dependency injection without custom attribute support in the language, but it's much more cumbersome. The user-defined attributes allow you to specify, in a simple declarative way, the runtime dependencies between various classes. Without it you have to build up those dependencies in code, using some sort of fluent interface or builder pattern.

So my question is, is there any plan for Swift to support user-created annotations, and annotation processing compilation stages?

-1 on user-created annotation.

I wrote Java since 90s and hate user-created annotation. Annotation is indeed a backdoor in syntax system, it makes source code totally un-understandable before you read reference of the annotation.

···

在 2016年1月14日,上午9:24,Talin via swift-evolution <swift-evolution@swift.org> 写道:

As a former Googler, I've spent a lot of years writing Java code that uses dependency injection, and this relies heavily on the ability to have custom annotations/attributes in the language - particularly, user-defined attributes on function parameters - and to generate additional code at compile time via annotation processors. Although dependency injection does have it's detractors, it's getting better (current best of breed is http://google.github.io/dagger/\), and it solves an amazing array of problems, including the ability for asynchronous programming to disappear into the underlying framework - you just write synchronous code and the framework handles the rest (no more futures!).

Now, you can of course do dependency injection without custom attribute support in the language, but it's much more cumbersome. The user-defined attributes allow you to specify, in a simple declarative way, the runtime dependencies between various classes. Without it you have to build up those dependencies in code, using some sort of fluent interface or builder pattern.

So my question is, is there any plan for Swift to support user-created annotations, and annotation processing compilation stages?

--
-- Talin
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Understood. I might add that one of the advantages of Dagger2 over the
older Guice framework is that it does all of the reflection work at compile
time, rather than at runtime, so a lot of the expense of runtime reflection
is avoided. This also means that the generated code for the injection logic
is debuggable, which was historically one of the biggest complaints about
Guice.

So the bottom line for me is, I don't care as much about the ability to
access the attributes at runtime - I'm more interested as to whether the
module artifacts output by the compiler can be introspected via some simple
API, or are stored in some relatively transparent encoding.

···

On Wed, Jan 13, 2016 at 5:58 PM, Chris Lattner <clattner@apple.com> wrote:

On Jan 13, 2016, at 5:24 PM, Talin via swift-evolution < > swift-evolution@swift.org> wrote:

As a former Googler, I've spent a lot of years writing Java code that uses
dependency injection, and this relies heavily on the ability to have custom
annotations/attributes in the language - particularly, user-defined
attributes on function parameters - and to generate additional code at
compile time via annotation processors. Although dependency injection does
have it's detractors, it's getting better (current best of breed is
http://google.github.io/dagger/\), and it solves an amazing array of
problems, including the ability for asynchronous programming to disappear
into the underlying framework - you just write synchronous code and the
framework handles the rest (no more futures!).

Now, you can of course do dependency injection without custom attribute
support in the language, but it's much more cumbersome. The user-defined
attributes allow you to specify, in a simple declarative way, the runtime
dependencies between various classes. Without it you have to build up those
dependencies in code, using some sort of fluent interface or builder
pattern.

So my question is, is there any plan for Swift to support user-created
annotations, and annotation processing compilation stages?

Hi Talin,

We have no concrete plans for user defined attributes, but it is a natural
extension. One of our goals for Swift 3 is to nail down the reflection
metadata representation. We should design this to be extensible to support
user defined attributes so that we don’t close this off in the future.

-Chris

--
-- Talin

You can say that about any metaprogramming feature, including operator
overloading, parametric polymorphism, automated code generation, or any
other form of programmable syntax - all of these features have to be used
with care because they essentially create new dialects of the language. As
for using annotations to drive dependency injection, this is used by
thousands of Google engineers on a daily basis and people generally find it
quite powerful and helpful. I actually think it is syntactically less
dangerous overall than the ability to invent new infix operators, because
at least you have an imported name that you can search for.

My own personal interest is in metaprogramming, that is "code that operates
on code", whether it be by reflection, template instantiation, or automated
code generation. In Java, this is sometimes done through reflection, and
sometimes via annotation processors, which are plugins to the Java compiler
that can examine the annotations and generate additional helper classes. By
contrast, in my own experimental language, the output of the compiler is
stored in a generic introspectable binary file format, similar to protocol
buffers, which can easily be taken apart and re-assembled by any
programming language (C, Python, Swift, Go, etc), allowing any sort of
post-processing on a compiled module.

···

On Wed, Jan 13, 2016 at 10:01 PM, Yang Wu <pinxue@gmail.com> wrote:

-1 on user-created annotation.

I wrote Java since 90s and hate user-created annotation. Annotation is
indeed a backdoor in syntax system, it makes source code totally
un-understandable before you read reference of the annotation.

在 2016年1月14日,上午9:24,Talin via swift-evolution <swift-evolution@swift.org>
写道:

As a former Googler, I've spent a lot of years writing Java code that uses
dependency injection, and this relies heavily on the ability to have custom
annotations/attributes in the language - particularly, user-defined
attributes on function parameters - and to generate additional code at
compile time via annotation processors. Although dependency injection does
have it's detractors, it's getting better (current best of breed is
http://google.github.io/dagger/\), and it solves an amazing array of
problems, including the ability for asynchronous programming to disappear
into the underlying framework - you just write synchronous code and the
framework handles the rest (no more futures!).

Now, you can of course do dependency injection without custom attribute
support in the language, but it's much more cumbersome. The user-defined
attributes allow you to specify, in a simple declarative way, the runtime
dependencies between various classes. Without it you have to build up those
dependencies in code, using some sort of fluent interface or builder
pattern.

So my question is, is there any plan for Swift to support user-created
annotations, and annotation processing compilation stages?

--
-- Talin
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
-- Talin

No, they are not same. Macro and preprocessor changes the language as well,
any RTTI supported feature doesn't, but only affects runtime behavior.
Operator overloading changes semantic of existed language element, but
doesn't enable introducing of any new syntax element.

Java's annotation allows you to add any @ beginning identifier as they are
part of the language. It is more like I cannot tell if a function call is
right or not before read the document of that library. But I wish we may
keep the language pre-defined.

PS, Java Annotation is obviously not equal to implement DI, AspectJ existed
before annotation was added and configuration file based methods existed
even longer.

···

On Thu, Jan 14, 2016 at 2:21 PM, Talin <viridia@gmail.com> wrote:

You can say that about any metaprogramming feature, including operator
overloading, parametric polymorphism, automated code generation, or any
other form of programmable syntax - all of these features have to be used
with care because they essentially create new dialects of the language. As
for using annotations to drive dependency injection, this is used by
thousands of Google engineers on a daily basis and people generally find it
quite powerful and helpful. I actually think it is syntactically less
dangerous overall than the ability to invent new infix operators, because
at least you have an imported name that you can search for.

My own personal interest is in metaprogramming, that is "code that
operates on code", whether it be by reflection, template instantiation, or
automated code generation. In Java, this is sometimes done through
reflection, and sometimes via annotation processors, which are plugins to
the Java compiler that can examine the annotations and generate additional
helper classes. By contrast, in my own experimental language, the output of
the compiler is stored in a generic introspectable binary file format,
similar to protocol buffers, which can easily be taken apart and
re-assembled by any programming language (C, Python, Swift, Go, etc),
allowing any sort of post-processing on a compiled module.

On Wed, Jan 13, 2016 at 10:01 PM, Yang Wu <pinxue@gmail.com> wrote:

-1 on user-created annotation.

I wrote Java since 90s and hate user-created annotation. Annotation is
indeed a backdoor in syntax system, it makes source code totally
un-understandable before you read reference of the annotation.

在 2016年1月14日,上午9:24,Talin via swift-evolution <swift-evolution@swift.org>
写道:

As a former Googler, I've spent a lot of years writing Java code that
uses dependency injection, and this relies heavily on the ability to have
custom annotations/attributes in the language - particularly, user-defined
attributes on function parameters - and to generate additional code at
compile time via annotation processors. Although dependency injection does
have it's detractors, it's getting better (current best of breed is
http://google.github.io/dagger/\), and it solves an amazing array of
problems, including the ability for asynchronous programming to disappear
into the underlying framework - you just write synchronous code and the
framework handles the rest (no more futures!).

Now, you can of course do dependency injection without custom attribute
support in the language, but it's much more cumbersome. The user-defined
attributes allow you to specify, in a simple declarative way, the runtime
dependencies between various classes. Without it you have to build up those
dependencies in code, using some sort of fluent interface or builder
pattern.

So my question is, is there any plan for Swift to support user-created
annotations, and annotation processing compilation stages?

--
-- Talin
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
-- Talin

--
Best Regards!

Yang Wu
--------------------------------------------------------
Location: Pudong, Shanghai, China.
EMail : pinxue@gmail.com
Website: http://www.time2change.mobi http://rockplayer.com
Twitter/Weibo : @pinxue
<http://www.pinxue.net>

Hi Chris,

  is there a document or notes on discussions on reflection metadata representations and an API to access them? We are very interested in this as it essential to have that make a powerful I/O system, like we have in ROOT, it is mandatory to be able to find out at run-time all possible object details. User defined attributes are in our case used to annotate e.g. transient data members that should not be streamed, or the precision with which a certain data member should be streamed (a double that could be streamed with a much lower precision saves a lot of bytes in the output, etc.).

Cheers, Fons.

···

On 13 Jan 2016, at 22:58, Chris Lattner via swift-evolution <swift-evolution@swift.org> wrote:

On Jan 13, 2016, at 5:24 PM, Talin via swift-evolution <swift-evolution@swift.org> wrote:

As a former Googler, I've spent a lot of years writing Java code that uses dependency injection, and this relies heavily on the ability to have custom annotations/attributes in the language - particularly, user-defined attributes on function parameters - and to generate additional code at compile time via annotation processors. Although dependency injection does have it's detractors, it's getting better (current best of breed is http://google.github.io/dagger/\), and it solves an amazing array of problems, including the ability for asynchronous programming to disappear into the underlying framework - you just write synchronous code and the framework handles the rest (no more futures!).

Now, you can of course do dependency injection without custom attribute support in the language, but it's much more cumbersome. The user-defined attributes allow you to specify, in a simple declarative way, the runtime dependencies between various classes. Without it you have to build up those dependencies in code, using some sort of fluent interface or builder pattern.

So my question is, is there any plan for Swift to support user-created annotations, and annotation processing compilation stages?

Hi Talin,

We have no concrete plans for user defined attributes, but it is a natural extension. One of our goals for Swift 3 is to nail down the reflection metadata representation. We should design this to be extensible to support user defined attributes so that we don’t close this off in the future.

-Chris

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--------------------------------------------------------------------------
Dr. Fons Rademakers CERN - European Organization for Nuclear Research
Chief Research Officer 1211 Geneve 23, Switzerland
CERN openlab Tel: +41227679248 Mobile: +41754113742
--------------------------------------------------------------------------

Understood. I might add that one of the advantages of Dagger2 over the older Guice framework is that it does all of the reflection work at compile time, rather than at runtime, so a lot of the expense of runtime reflection is avoided. This also means that the generated code for the injection logic is debuggable, which was historically one of the biggest complaints about Guice.

So the bottom line for me is, I don't care as much about the ability to access the attributes at runtime - I'm more interested as to whether the module artifacts output by the compiler can be introspected via some simple API, or are stored in some relatively transparent encoding.

Ah I see, I don’t know anyone considering building those sorts of tools right now.

-Chris

···

On Jan 13, 2016, at 6:18 PM, Talin <viridia@gmail.com> wrote:

On Wed, Jan 13, 2016 at 5:58 PM, Chris Lattner <clattner@apple.com <mailto:clattner@apple.com>> wrote:

On Jan 13, 2016, at 5:24 PM, Talin via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

As a former Googler, I've spent a lot of years writing Java code that uses dependency injection, and this relies heavily on the ability to have custom annotations/attributes in the language - particularly, user-defined attributes on function parameters - and to generate additional code at compile time via annotation processors. Although dependency injection does have it's detractors, it's getting better (current best of breed is http://google.github.io/dagger/\), and it solves an amazing array of problems, including the ability for asynchronous programming to disappear into the underlying framework - you just write synchronous code and the framework handles the rest (no more futures!).

Now, you can of course do dependency injection without custom attribute support in the language, but it's much more cumbersome. The user-defined attributes allow you to specify, in a simple declarative way, the runtime dependencies between various classes. Without it you have to build up those dependencies in code, using some sort of fluent interface or builder pattern.

So my question is, is there any plan for Swift to support user-created annotations, and annotation processing compilation stages?

Hi Talin,

We have no concrete plans for user defined attributes, but it is a natural extension. One of our goals for Swift 3 is to nail down the reflection metadata representation. We should design this to be extensible to support user defined attributes so that we don’t close this off in the future.

-Chris

--
-- Talin

Hi Chris,

is there a document or notes on discussions on reflection metadata representations and an API to access them? We are very interested in this as it essential to have that make a powerful I/O system, like we have in ROOT, it is mandatory to be able to find out at run-time all possible object details. User defined attributes are in our case used to annotate e.g. transient data members that should not be streamed, or the precision with which a certain data member should be streamed (a double that could be streamed with a much lower precision saves a lot of bytes in the output, etc.).

Hi Fons,

There is some early documentation here:
https://github.com/apple/swift/blob/master/docs/ABI.rst#type-metadata

Slava is the one working on this area IIRC.

-Chris

···

On Jan 19, 2016, at 7:10 AM, Fons Rademakers <Fons.Rademakers@cern.ch> wrote:

On 13 Jan 2016, at 22:58, Chris Lattner via swift-evolution <swift-evolution@swift.org> wrote:

On Jan 13, 2016, at 5:24 PM, Talin via swift-evolution <swift-evolution@swift.org> wrote:

As a former Googler, I've spent a lot of years writing Java code that uses dependency injection, and this relies heavily on the ability to have custom annotations/attributes in the language - particularly, user-defined attributes on function parameters - and to generate additional code at compile time via annotation processors. Although dependency injection does have it's detractors, it's getting better (current best of breed is http://google.github.io/dagger/\), and it solves an amazing array of problems, including the ability for asynchronous programming to disappear into the underlying framework - you just write synchronous code and the framework handles the rest (no more futures!).

Now, you can of course do dependency injection without custom attribute support in the language, but it's much more cumbersome. The user-defined attributes allow you to specify, in a simple declarative way, the runtime dependencies between various classes. Without it you have to build up those dependencies in code, using some sort of fluent interface or builder pattern.

So my question is, is there any plan for Swift to support user-created annotations, and annotation processing compilation stages?

Hi Talin,

We have no concrete plans for user defined attributes, but it is a natural extension. One of our goals for Swift 3 is to nail down the reflection metadata representation. We should design this to be extensible to support user defined attributes so that we don’t close this off in the future.

-Chris

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--------------------------------------------------------------------------
Dr. Fons Rademakers CERN - European Organization for Nuclear Research
Chief Research Officer 1211 Geneve 23, Switzerland
CERN openlab Tel: +41227679248 Mobile: +41754113742
--------------------------------------------------------------------------

Hi Chris,

  many thanks for the pointer. Will there also be a method or function calling interface, so interfaces can be discovered and called at run-time. Something we abundantly use via the Cling interpreter.

Cheers, Fons.

···

On 19 Jan 2016, at 15:20, Chris Lattner <clattner@apple.com> wrote:

On Jan 19, 2016, at 7:10 AM, Fons Rademakers <Fons.Rademakers@cern.ch> wrote:

Hi Chris,

is there a document or notes on discussions on reflection metadata representations and an API to access them? We are very interested in this as it essential to have that make a powerful I/O system, like we have in ROOT, it is mandatory to be able to find out at run-time all possible object details. User defined attributes are in our case used to annotate e.g. transient data members that should not be streamed, or the precision with which a certain data member should be streamed (a double that could be streamed with a much lower precision saves a lot of bytes in the output, etc.).

Hi Fons,

There is some early documentation here:
https://github.com/apple/swift/blob/master/docs/ABI.rst#type-metadata

Slava is the one working on this area IIRC.

-Chris

On 13 Jan 2016, at 22:58, Chris Lattner via swift-evolution <swift-evolution@swift.org> wrote:

On Jan 13, 2016, at 5:24 PM, Talin via swift-evolution <swift-evolution@swift.org> wrote:

As a former Googler, I've spent a lot of years writing Java code that uses dependency injection, and this relies heavily on the ability to have custom annotations/attributes in the language - particularly, user-defined attributes on function parameters - and to generate additional code at compile time via annotation processors. Although dependency injection does have it's detractors, it's getting better (current best of breed is http://google.github.io/dagger/\), and it solves an amazing array of problems, including the ability for asynchronous programming to disappear into the underlying framework - you just write synchronous code and the framework handles the rest (no more futures!).

Now, you can of course do dependency injection without custom attribute support in the language, but it's much more cumbersome. The user-defined attributes allow you to specify, in a simple declarative way, the runtime dependencies between various classes. Without it you have to build up those dependencies in code, using some sort of fluent interface or builder pattern.

So my question is, is there any plan for Swift to support user-created annotations, and annotation processing compilation stages?

Hi Talin,

We have no concrete plans for user defined attributes, but it is a natural extension. One of our goals for Swift 3 is to nail down the reflection metadata representation. We should design this to be extensible to support user defined attributes so that we don’t close this off in the future.

-Chris

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--------------------------------------------------------------------------
Dr. Fons Rademakers CERN - European Organization for Nuclear Research
Chief Research Officer 1211 Geneve 23, Switzerland
CERN openlab Tel: +41227679248 Mobile: +41754113742
--------------------------------------------------------------------------

--------------------------------------------------------------------------
Dr. Fons Rademakers CERN - European Organization for Nuclear Research
Chief Research Officer 1211 Geneve 23, Switzerland
CERN openlab Tel: +41227679248 Mobile: +41754113742
--------------------------------------------------------------------------

BTW other use cases for this sort of thing are automated generation of
testing mocks and RPC stubs - things that require a lot of exacting,
repetitive drudgery and that no programmer should have to code manually.
It's possible to do these with reflection as well, however the problem with
reflection is that when you look at the code in the debugger you have to
peer inside a lot of obscure data structures to figure out what is going
on, whereas it's fairly intuitive to step through generated source code.

···

On Wed, Jan 13, 2016 at 9:55 PM, Chris Lattner <clattner@apple.com> wrote:

On Jan 13, 2016, at 6:18 PM, Talin <viridia@gmail.com> wrote:

Understood. I might add that one of the advantages of Dagger2 over the
older Guice framework is that it does all of the reflection work at compile
time, rather than at runtime, so a lot of the expense of runtime reflection
is avoided. This also means that the generated code for the injection logic
is debuggable, which was historically one of the biggest complaints about
Guice.

So the bottom line for me is, I don't care as much about the ability to
access the attributes at runtime - I'm more interested as to whether the
module artifacts output by the compiler can be introspected via some simple
API, or are stored in some relatively transparent encoding.

Ah I see, I don’t know anyone considering building those sorts of tools
right now.

-Chris

On Wed, Jan 13, 2016 at 5:58 PM, Chris Lattner <clattner@apple.com> wrote:

On Jan 13, 2016, at 5:24 PM, Talin via swift-evolution < >> swift-evolution@swift.org> wrote:

As a former Googler, I've spent a lot of years writing Java code that
uses dependency injection, and this relies heavily on the ability to have
custom annotations/attributes in the language - particularly, user-defined
attributes on function parameters - and to generate additional code at
compile time via annotation processors. Although dependency injection does
have it's detractors, it's getting better (current best of breed is
http://google.github.io/dagger/\), and it solves an amazing array of
problems, including the ability for asynchronous programming to disappear
into the underlying framework - you just write synchronous code and the
framework handles the rest (no more futures!).

Now, you can of course do dependency injection without custom attribute
support in the language, but it's much more cumbersome. The user-defined
attributes allow you to specify, in a simple declarative way, the runtime
dependencies between various classes. Without it you have to build up those
dependencies in code, using some sort of fluent interface or builder
pattern.

So my question is, is there any plan for Swift to support user-created
annotations, and annotation processing compilation stages?

Hi Talin,

We have no concrete plans for user defined attributes, but it is a
natural extension. One of our goals for Swift 3 is to nail down the
reflection metadata representation. We should design this to be extensible
to support user defined attributes so that we don’t close this off in the
future.

-Chris

--
-- Talin

--
-- Talin

There will certainly be a reflection API at some point, but it hasn’t been scheduled. I’ve been working under the assumption that it won’t fit into Swift 3, but we’ll see how things go. Once the metadata format is nailed down, it can be added at any time.

-Chris

···

On Jan 21, 2016, at 9:45 AM, Fons Rademakers <Fons.Rademakers@cern.ch> wrote:

Hi Chris,

many thanks for the pointer. Will there also be a method or function calling interface, so interfaces can be discovered and called at run-time. Something we abundantly use via the Cling interpreter.