Storing data within annotations
Similar to a method, an annotation supports both positional and named arguments:
annotation MyAnnotation end @[MyAnnotation(name: "value", id: 123)] def foo; end @[MyAnnotation("foo", 123, false)] def bar; end
In this example, we defined two empty methods, where each method has an annotation applied to it. The first one is solely using named arguments, while the second is using solely positional arguments. A better example of applying multiple annotations of the same type can be demonstrated when each annotation has data included within it. Here is an example:
annotation MyAnnotation; end @[MyAnnotation(1, enabled: false)] @[MyAnnotation(2)] def foo end
As the values on each annotation can be different, the related library could create multiple methods or variables, for example, based on each annotation and the data within it. However, this data isn't any good if you cannot access it! Let's take a look at how...